0

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?

Community
  • 1
  • 1
  • 1
    Do any errors appear in your [browser console](https://developers.google.com/chrome-developer-tools/docs/console)? – Alex Wayne Nov 21 '12 at 18:38
  • I think this SO answer will solve your problem: http://stackoverflow.com/questions/236073/why-split-the-script-tag-when-writing-it-with-document-write – Kyle Nov 21 '12 at 18:44

4 Answers4

1

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>
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • +1 for avoiding document.write. Leaving the document stream open like that is not a good idea. – Kyle Nov 21 '12 at 18:48
0

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

Frank Natividad
  • 604
  • 6
  • 17
  • To answer @JoelEtherton 's question, browsers support just using `//` instead of specifying http or https. The content will be loaded using whatever current connection type you have. – Kyle Nov 21 '12 at 18:40
0

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.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • i thing you are missing something here. The window.open statement is in a script on a server, and the html file is on my local computer. I want to execute that script on the server so adding onload=".." to the body tag is not what am looking for because the function you are talking about is on a different server and not on the same machine as the html file – Alex Morgan Nov 21 '12 at 18:51
  • The location of the script is irrelevant. The entire document is parsed together in the browser (your machine) once all referenced files are downloaded. As the document is downloaded and parsed, it will execute any functions that it is instructed to execute. In this particular case, because you're using document.write to provide the reference, the instructions in the script will not automatically process because the event that would normally cause them to execute has already been called. – Joel Etherton Nov 21 '12 at 18:57
0
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.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337