HTML variables in javascript and embedding HTML in javascript
I’m messing about with my homepage and tarting it up with a bit of javascript, which is new to me. I found it pretty easy but two things were difficult and took a huge amount of googling to get right.
First, I wanted to open a new window with a jpg in it but I wanted the window to be sized according to the size of the jpg. I couldn’t figure how to pass the height and width variables to the javascript function. I finally did it by wrapping the variable with +’s like this ‘+variable+’. Here is the javascript function code -
function pop(url, high, wide) {
newwindow=window.open(url,'','height='+high+',width='+wide+'');
}
Secondly, and I guess most of you know this, but you can embed HTML commands into javascript by typing them into a string like this -
var browserName = navigator.appName
if (browserName=="Microsoft Internet Explorer") {
document.write("You are still using Microsoft Explorer, you should upgrade to Firefox as soon as possible. Follow this link" + '<a href="http://www.mozilla.com/en-US/firefox/all.html" target="blank"><img alt="Firefox" src="Firefox.png"></a>')
}
else {
document.write("I can see that you are an experienced surfer because you are not using Microsoft Explorer" + '<a href="http://www.mozilla.com/en-US/firefox/all.html" target="blank"><img alt="NoIE" src="NoIE.jpg"></a>')
}
That’s all for now.