Log in

View Full Version : Adding Adding onclick=window.location within document.write tag



sharma.raj411
06-30-2012, 04:32 AM
I am trying to put the following a href tag into a document.write tag and I can't seem to get the syntax right or know if it is possible to put another javascript function call into a different javascript:

This is the tag that would rest in a lone href tag:

<a href="#" onclick="window.location=y">Test</a>

Here y is a javascript variable with some url.

And this is how I tried to enter the tag:

document.write("<a href=# onclick=window.location='+ y +'>Test</a>");

Any help would be MUCH appreciated.

jscheuer1
06-30-2012, 06:33 AM
document.write('<a href="#" onclick="window.location = ' + y + '; return false;">Test</a>');

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.

sharma.raj411
06-30-2012, 07:34 AM
I tried your code John, but it dont seem to work well.

Here is my code snippet:

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
var y=x[0].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;
alert(y);
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
document.write('<a href="#" onclick="window.location = ' + y + '; return false;">Test</a>');
</script>

</body>
</html>

I am basically trying to read the contents of an xml file and storing it in variable y. and using that as an href.

When in a lone a href tag, it works fine and www.google.com opens up just fine, as the value of y is www.google.com, but the same fails in document.write.

jscheuer1
06-30-2012, 08:31 AM
Ooops, that should be:


document.write('<a href="#" onclick="window.location = \'' + y + '\'; return false;">Test</a>');

However, you could get the same basic result like so:


document.write('<a href="' + y + '">Test</a>');

The only difference being that the user would be able to see the href (in browsers that do this) in the browser's status bar or equivalent when hovering the link.

sharma.raj411
06-30-2012, 03:45 PM
Thanks a lot John, your code works..

molendijk
06-30-2012, 06:56 PM
John, shouldn't that be (with a back slash):

document.write('<a href="' + y + '">Test<\/a>'); ?
===
Arie.

jscheuer1
06-30-2012, 07:27 PM
John, shouldn't that be (with a back slash):

document.write('<a href="' + y + '">Test<\/a>'); ?
===
Arie.

In my experience - only if the code is on the page, and only if it's causing HTML/XHTML validation errors, and only if you care about that.

The only closing HTML tag as a string inside a script that absolutely needs to be escaped like that on the page is the closing </script> tag. I'm unsure if it needs to be in an external script or not. I just checked - Firefox, IE 9 in all its modes, Opera, Chrome don't mind:

ext.js:


document.write('<script>alert("hi");</script>');

ext_test.htm:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="ext.js"></script>
</head>
<body>

</body>
</html>

However, in a sense you're right. It never hurts to escape those slashes and neglecting to do so can at times cause one sort of problem or another depending upon circumstances.

molendijk
06-30-2012, 07:44 PM
In my experience - only if the code is on the page, and only if it's causing HTML/XHTML validation errors, and only if you care about that.
The only closing HTML tag as a string inside a script that absolutely needs to be escaped like that on the page is the closing </script> tag. I'm unsure if it needs to be in an external script or not. I just checked - Firefox, IE 9 in all its modes, Opera, Chrome don't mind
Another good reason to use external scripts instead of internal ones.
===
Arie.