Jump to content

JS: variable in HTML


tinfanide

Recommended Posts

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script>setTimeout(function redirect(){	location.href = "http://www.google.com/";	},5000);	function redirectDesc(url){	var p = document.createElement('p');	p.innerHTML = "The page will be redirected to <a href = 'java script:redirect()'>Google</a> in about 5 seconds.";	document.body.appendChild(p);	}	window.onload = redirectDesc;</script>	</head><body></body></html>

I want to use the variable value to replace manually typing the address in where Google is written in the innerHTML.I have tried that it's not possible to just type the variable url within the innerHTML.Any way to achieve that (to make the innerHTML between the anchor tags be http://www.google.com/?

Link to comment
Share on other sites

where did you get "document.createElement" from ?is that a built-in function of the DOM ?i don't see it in this list;http://www.w3schools.com/jsref/dom_obj_all.asp
It may not be within JS, or it should be, I should say, coz it runs well in both IE & Firefox.See here:http://www.w3schools.com/dom/met_document_createelement.aspBut I didn't learn it there. I picked it up in some of the previous posts from the folks helping out in this forum.http://w3schools.invisionzone.com/index.ph...c=37893&hl=I have no idea of what DOMs are. I've heard about it when learning JS, though.
Link to comment
Share on other sites

Just make url global variable and use it for redirect and innerHTML of anchor

var url="http://www.google.com/";setTimeout(function redirect(){	location.href = url;	},5000);	function redirectDesc(){	var p = document.createElement('p');	p.innerHTML = "The page will be redirected to <a href = 'java script:redirect()'>"+url+"</a> in about 5 seconds.";	document.body.appendChild(p);	}	window.onload = redirectDesc;

Link to comment
Share on other sites

Just make url global variable and use it for redirect and innerHTML of anchor
var url="http://www.google.com/";setTimeout(function redirect(){	location.href = url;	},5000);	function redirectDesc(){	var p = document.createElement('p');	p.innerHTML = "The page will be redirected to <a href = 'java script:redirect()'>"+url+"</a> in about 5 seconds.";	document.body.appendChild(p);	}	window.onload = redirectDesc;

Thanks for that.I'd just messed up with some other bits and it appeared to be not working even I set the global variable.But later on I realised why was that and the global variable worked as expected. Just a minor error caused that.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...