/    Sign up×
Community /Pin to ProfileBookmark

Javascript code in a popup Window generated with javascript code

Hello Webdevelopers,

this thread is a continuation of the “Need Help to understand browsers behaviors”. I am creating a new thread because I found old title not very clear.

My objective is to call from an js source file the algorithm to form an string because it is pretty complex and We want to support it on a javascript file instead of support the string that will be printed in the newWindow. Let’s first start with the simplest code, the one that is used to call the popup window:

[code=html]

<html>
<head>
<script language=”JavaScript”>
function displayPopupWin() {
newwin = window.open();
newHtml = “<html> n” +
“<body> n” +
“<script language=’javascript’> n” +
“t document.write(‘Hello’); n” +
“</scr” + “ipt> n” +
“</body> n” +
“</html> n”;
newwin.document.open();
newwin.document.write(newHtml);
newwin.document.close();
}
</script>
</head>
<body>
<a href=”#” onClick=”javascript:displayPopupWin()”>Click here to display Popup Window</a>
</body>
</html>

[/code]

The code above creates a web page with a link to call the displayPopupWin() function, that generates the new Window.

The source code on IE6.2 is:

[code=html]
<html>
<body>
<script language=’javascript’>
document.write(‘Hello’);
</script>
</body>
</html>
[/code]

and in FireFox 1,5 is:
[code=html]
<html>
<body>
<script language=’javascript’>
document.write(‘Hello’);
</script>
</body>
</html>
Hello **********
[/code]

**********Notice the extra Hello at the end of the source code.

It works perfect in both browser, but, by looking on the source code, We are only printing a hardcoded string.

Now, let’s try to write the content of the page from the JS source file:

The webpage code is:

[code=html]
<html>
<head>
<script language=”JavaScript” src=”myJSSource.js”></script>
<script language=”JavaScript”>
function displayPopupWin() {
newwin = window.open();
newHtml = “<html> n” +
“<body> n” +
“<script language=’javascript’ src=’myJSSource.js’></scr” + “ipt> n” +
“<script language=’javascript’> n” +
“t document.write(myComplexContent()); n” +
“</scr” + “ipt> n” +
“</body> n” +
“</html> n”;
newwin.document.open();
newwin.document.write(newHtml);
newwin.document.close();
}
</script>
</head>
<body>
<a href=”#” onClick=”javascript:displayPopupWin()”>Click here to display Popup Window</a>
</body>
</html>
[/code]

and the myJSSource.js code is:

[CODE]
function myComplexContent() {
return “Hello”;
}
[/CODE]

The result is:
IE6.2:
The “Display a notification about every script error” returns: “Error: Object expected” in the line that contains myComplexContent() function. After that, if I refresh the generated page everything works fine, and the source code [B]after refreshing[/B] is:

[CODE]
<html>
<body>
<script language=’javascript’ src=’myJSSource.js’></script>
<script language=’javascript’>
document.write(myComplexContent());
</script>
</body>
</html>
[/CODE]

FireFox 1.5:
The browser display the correct result (it prints the value returning for the myComplexContent() function on the screen) but it looks like it is not finishing to load the webpage, and it doesn’t display code in the view source window.

Ok, let me know if the explanation of the problem is clear, any help and comment is welcome, also comments about my writting are welcome, English is not my first language and I want to improve it.

Regards,

Jaime

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 20.2006 — Try changing this part:
<i>
</i>newHtml = "&lt;html&gt; n" +
"&lt;body&gt; n" +
"&lt;script language='javascript' src='myJSSource.js'&gt;&lt;/scr" + "ipt&gt; n" +
"&lt;script language='javascript'&gt; n" +
"t document.write(myComplexContent()); n" +
"&lt;/scr" + "ipt&gt; n" +
"&lt;/body&gt; n" +
"&lt;/html&gt; n";
newwin.document.open();
newwin.document.write(newHtml);
newwin.document.close();

to more like this:
<i>
</i>var str = '';
str += '&lt;html&gt;n';
str += '&lt;head&gt;n';
str += '&lt;script src="myJSSource.js" type="text/javascript"&gt;&lt;/script&gt;n';
str += '&lt;/head&gt;n';
str += '&lt;body&gt;n';
str += '&lt;script type="text/javascript"&gt;n';
str += 'document.write(myComplexContent());n';
str += '&lt;/script&gt;n';
str += '&lt;/body&gt;n';
str += '&lt;/html&gt;n';
newwin.document.open();
newwin.document.write(str);
newwin.document.close();

... it doesn't display code in the view source window.[/QUOTE]
That is not surprising, either. It is often true that generated source does not show when using the browser's "View Source" tool.
Copy linkTweet thisAlerts:
@ananiasJul 03.2006 — I'm having the same problem. I have a client side script that reads a monthly television schedule via xmlhttp and creates a web page as a way to save the information locally (in a manner that permits additional processing.)

But I haven't found a way of saving the newly generated page (which is essentially the same as the old one except that it has another month's worth of scheduling info wired into it.)

I would like to know if you have made any progress on this problem?
×

Success!

Help @jbarragan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...