Steve’s Ubuntu Weblog

Mainly (but not only) about Ubuntu

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.

11 April, 2007 - Posted by steveyoung | Ubuntu | | 4 Comments

4 Comments »

  1. Steve, I just bought a wireless router and a PS121. After spending a few hours trying to get it to work (and not being real IT competent) I went to bed totally fustrated. This morning I woke up and went to Google and “rolled the dice”. Your sight came up and I followed the intructions and I happy to say that althought I don’t understand what I did – IT WORKED. Thanks Jimmie

    Comment by Jimmie | 13 April, 2007 | Reply

  2. I have tried for embed HTML commands into javascript in one of my application on local server and it works successfully.

    Thanks And Regards
    Bhakti

    Comment by Bhakti | 27 June, 2008 | Reply

  3. Thanks!

    I had been searching for a while on how to embed HTML in my Javascript (rather than the other way around). This finally showed me how to do it.

    Comment by Wolf | 27 October, 2008 | Reply

  4. Another appreciative Java newbie here. Don’t know why this wasn’t in any of the other Java tutorials, but thanks. It made it work.

    Comment by Andrew | 3 April, 2009 | Reply


Leave a comment