Alex Morgan
Alex Morgan

Reputation: 1

Website Data Through Javascript

i read this post and am trying to do the same but its not working. My javascript file "myscript.js" on the server is

/*var win;
function openWindow(){
 win = */

window.open("http://www.yahoo.com","","menubar=no,toolbar=no,width=400,height=600");

/*}

function closeWindow(){
win.close();
}*/

The server is a real server and not a local host server.

The full path to the file is http://eddyfreeman.woelmuis.nl/myscript.js and am trying to run the following page from my local computer so that the the javascript file will open a new window from the following page ::

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/Javascript">
<!--
  document.write("<script type='text/javascript' src='" + document.location.protocol + "://www.eddyfreeman.woelmuis.nl/myscript.js'></script>"); 

    //-->
</script>
</head>

<body>
<h1>PAGE TESTING</h1>
</body>
</html>

but its not working. I only see the text TESTING PAGE instead of a window opening before the text.

What am i doing wrong?

Upvotes: 0

Views: 63

Answers (4)

Alex Wayne
Alex Wayne

Reputation: 187262

var url = window.location.protocol + "//www.eddyfreeman.woelmuis.nl/myscript.js";
document.write("<script type='text/javascript' src='" + url + "'></scr"+"ipt>");​

http://jsfiddle.net/6STLc/1/

A few things.

protocol includes a colon. So window.location.protocol + "://foo.com" yields "http:://foo.com which isn't good.

Second, you have to careful when the characters </script> appear in a block of javascript, even in a string. The DOM parser notices that and thinks you are closing the script tag. If you are in the middle of a statement or string, this is obviously bad. It's common to breakup the closing script tag into 2 strings to avoid this problem.

And avoid using document.write, but I won't go into that in my answer.

Upvotes: 0

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54820

You should use protocol relative URLs and avoid using document.write to add your script tag:

<script type='text/javascript' src='//www.eddyfreeman.woelmuis.nl/myscript.js'></script>

Upvotes: 1

Joel Etherton
Joel Etherton

Reputation: 37543

You're expecting something to happen that you aren't telling to happen. First you're using document.write to include a script which is not appropriate. This means that the load has already occurred (that's the document.write portion) so what's included in your script can't be parsed. If you want to do it this way, you'll need to make use of a function instead.

In your script file:

function openWindow() {
    window.open("http://www.yahoo.com","","menubar=no,toolbar=no,width=400,height=600");
}

In your html file:

<body onload="openWindow()">

However, it would be better if you used jQuery to do this. This may be a little outside of your javascript comfort zone, however.

Upvotes: 0

Frank Natividad
Frank Natividad

Reputation: 624

Just change your document.location.protocol to "http"

Upvotes: 0

Related Questions