My Custom Rack I Built
Reply to topic
 
image tag script [resolved]

Junior Member(L1)

Junior Member(L1)

Joined:16 Apr 2004
Posts:13
Reply with quote
Can anyone tell me how to stop the script below from removing the back slashes in the image URL? Here's the result I get in the alert:

<img src=C:WindowsDesktopimage1.jpg width="100" height="100" border="0">

function getImageDims(imgurl)
{
var oImage = new Image();
oImage.onload = new Function(
'alert("<img src='+imgurl + ' width="+this.width + " height="+this.height + [" border=0>"])'
);
oImage.src = load.value;
}
</script>
</head>
<body>
<input type="file" name="load"> <input type="button" value="Get Image Data" onclick="getImageDims(load.value)">

The URL is displayed correctly with slashes in the <input type="file" text field, but the script changes load.value by removing the slashes.
View user's profileFind all posts by starrwriterSend private message
image tag script [resolved]

Moderator

Moderator

Joined:23 Sep 2002
Posts:949
Location:St. Louis, MO
Reply with quote
To JavaScript, "\" is a special character referred to as Escapse. It is used to indicate that the next character is something special. So to indicate that the \ is the one you want, you have to escape it like \\. Before you submit if you were to go through and put in \\ wherever there is a \, it would work. This is the same for all C languages.

Yeah, it is a pain to have to go through and manually fix the \ so, to make it easier, you need to use a replace statement as I did below. It uses regular expressions which is a totally different topic. The four slashes are actually two sets of \\.

Code:

<SCRIPT LANGUAGE="JavaScript">
function getImageDims(imgurl)
{
   imgurl = imgurl.replace(/\\/g, "\\\\");
   var oImage = new Image();
   oImage.onload = new Function('alert("<img src='+imgurl + ' width="+this.width + " height="+this.height + [" border=0>"])');
   oImage.src = load.value;
}
</script>
</head>
<body>
<input type="file" name="load"> <input type="button" value="Get Image Data" onclick="getImageDims(load.value)">

_________________
Sometimes you feel like a nut, and sometimes you don't.
View user's profileFind all posts by EricMEvansSend private messageAIM AddressYahoo MessengerICQ Number
image tag script [resolved]

Junior Member(L1)

Junior Member(L1)

Joined:16 Apr 2004
Posts:13
Reply with quote
EricMEvans wrote:
To JavaScript, "\" is a special character referred to as Escapse. It is used to indicate that the next character is something special. So to indicate that the \ is the one you want, you have to escape it like \\. Before you submit if you were to go through and put in \\ wherever there is a \, it would work. This is the same for all C languages.

Yeah, it is a pain to have to go through and manually fix the \ so, to make it easier, you need to use a replace statement as I did below. It uses regular expressions which is a totally different topic. The four slashes are actually two sets of \\.

Code:

<SCRIPT LANGUAGE="JavaScript">
function getImageDims(imgurl)
{
   imgurl = imgurl.replace(/\\/g, "\\\\");
   var oImage = new Image();
   oImage.onload = new Function('alert("<img src='+imgurl + ' width="+this.width + " height="+this.height + [" border=0>"])');
   oImage.src = load.value;
}
</script>
</head>
<body>
<input type="file" name="load"> <input type="button" value="Get Image Data" onclick="getImageDims(load.value)">


That works, but I forgot about the quotes that go around the image URL in an image tag. I'll figure it out. Thanks for your help.
View user's profileFind all posts by starrwriterSend private message
image tag script [resolved]

Moderator

Moderator

Joined:23 Sep 2002
Posts:949
Location:St. Louis, MO
Reply with quote
Here it is with the other quotes

Code:

<SCRIPT LANGUAGE="JavaScript">
function getImageDims(imgurl)
{
   imgurl = imgurl.replace(/\\/g, "\\\\");
   var oImage = new Image();
   oImage.onload = new Function('alert("<img src=\\"'+imgurl + '\\" width="+this.width + " height="+this.height + [" border=0>"])');
   oImage.src = load.value;
}
</script>
</head>
<body>
<input type="file" name="load"> <input type="button" value="Get Image Data" onclick="getImageDims(load.value)">

_________________
Sometimes you feel like a nut, and sometimes you don't.
View user's profileFind all posts by EricMEvansSend private messageAIM AddressYahoo MessengerICQ Number
image tag script [resolved]
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum
All times are GMT - 6 Hours  
Page 1 of 1  


  
  
 Reply to topic