0

I'm trying to use the value of a JavaScript variable; however, for some reason, document.write is not working.

Can you tell me what am I doing wrong and if there's another way to do this? Here is my JavaScript code:

<script type="text/javascript">
    var url=${paramValue};
    url = url.replace(/\s/g,"%20");
    document.write("<a href='" + url + "'>" +
        "<img src='/folder1/folder2/folder3/folder4/folder5/image.png'" +
        " width='70' height='20'></a>");
</script>

The variable url=${paramValue}; is working, as I can check it with firebug. The image is not appearing.

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54

3 Answers3

2

What is parsing your url variable? "${paramValue}" is not going to be interpreted by the JS interpreter.

Don't bother manually decoding/encoding URLs in Javascript...just use the built in:

encodeURIComponent(str) encodeURI(str)

Encode URL in JavaScript?

Community
  • 1
  • 1
HankHendrix
  • 295
  • 2
  • 9
0

I think you need quotes around your parameter, so the javascript interprets it as a string after the replacement:

var url='${paramValue}';

But I agree with Hank - use the built in functions.

Mike C
  • 3,077
  • 22
  • 29
-1
<html> 
<head>
<script type="text/javascript"> 
function myfunction(){
    var url = $("#<%=url.ClientID %>").val();
    document.write("<a href='" + url + "'><img src='abc.jpg' width='70'height='20'>    
    </a>");
}
</script>
</head>
<body>
<form runat="server">
   <asp:TextBox ID="url" runat="server" />
   <input type="button" value="Clear" onclick="myfunction()" />
</form>
</body>  
</html>
Hiren
  • 41
  • 1