innerHTML and variable scope

N

Nick

OK. I've had a trawl through past messages but can see nothing that
helps me, so...here goes...

I am writing a web application which, among other things, pushes a web
page to a user's browser.

In very simple terms:

- I have a script in hidden frame A which sets the document.location
of main frame B.

- Once the document has been loaded in frame B, the script updates the
document.body.innerHTML of that document.

(Pushing the URL alone is not enough, since I want to push values in
form fields etc)

This model works great if I am pushing simple web pages, but as soon
as the page I am pushing has complex javascript in it, bad things
start happening.

One such page has a load of js file includes, which are used for the
drop down menus. Loaded normally in the browser, all the js on this
page works fine. However, if the page has been pushed to the browser
using my system, I get lots of '(varname) is null or not an object'
errors when I try to use the menus.

Now this smacks of a var scope problem, or somesuch. But I have no
real idea.

Can anyone suggest what might be happening here, or has anyone come
across this problem before?

I can post up some code if you want, but I thought I'd just get a
general idea if anyone has come across this problem first.

Cheers in advance for any ideas thrown into the ring!
 
R

Randy Webb

Nick wrote:

- Once the document has been loaded in frame B, the script updates the
document.body.innerHTML of that document.

This model works great if I am pushing simple web pages, but as soon
as the page I am pushing has complex javascript in it, bad things
start happening.

Can anyone suggest what might be happening here, or has anyone come
across this problem before?

Your problem is not one of scope, but of execution. If you "push" this code:

<script type="text/javascript">
alert('It executed');
</script>

You won't get the alert because it doesn't get executed.

Some possibilities:

document.write the frame instead of innerHTML.
Use createElement to create a script element and then append (as a
child), the data, or a src="someFile.js" attribute.
 
N

Nick

Your problem is not one of scope, but of execution. If you "push" this code:
<script type="text/javascript">
alert('It executed');
</script>

You won't get the alert because it doesn't get executed.

Some possibilities:

document.write the frame instead of innerHTML.
Use createElement to create a script element and then append (as a
child), the data, or a src="someFile.js" attribute.

OK Randy, I was doing some thinking last night, and thought I'd try
chasing up document.write as well. As an experiment, I took the source
code of my problematic page with pulldown menus, stuck it all in a
textarea, and wrote a very short script to document.write the contents
of that field out to another frame.

top.frames['two'].document.open("about:blank");
top.frames['two'].document.writeln(document.daform.sourcecode.value);
top.frames['two'].document.close();

But it doesn't work! Again the problem is with the javascript in the
page being pushed. In the main body of this document, there is an
inline call to a js function which is defined in an external file
referenced in the HEAD. I get an "object expected" error meaning that
it can't find the function called on that line.

I'm guessing that when a document is built using document.write() the
different parts of the page - inline js, external js etc - are
executed in a different order.

Funnily enough, when I refresh the page that has been pushed, it WORKS
ABSOLUTELY FINE! So obviously the browser has built the page in the
right order on refresh.

Oh yeah: I tried splitting the source code into lines (using "split"
on newlines), and then writing out each line separately. I thought if
I wrote each line separately, it might affect the "parse order".

IE crashed after a few lines :(

Thanks for your other suggestions, too, but I feel that if I could get
either the document.location/innerHTML or document.write() models
working, that would be the best course.

Any further suggestions?

Cheers for your continuing help.

Nick
 
R

Randy Webb

Nick said:
Your problem is not one of scope, but of execution. If you "push" this code:

<script type="text/javascript">
alert('It executed');
</script>

You won't get the alert because it doesn't get executed.

Some possibilities:

document.write the frame instead of innerHTML.
Use createElement to create a script element and then append (as a
child), the data, or a src="someFile.js" attribute.


OK Randy, I was doing some thinking last night, and thought I'd try
chasing up document.write as well. As an experiment, I took the source
code of my problematic page with pulldown menus, stuck it all in a
textarea, and wrote a very short script to document.write the contents
of that field out to another frame.

top.frames['two'].document.open("about:blank");
top.frames['two'].document.writeln(document.daform.sourcecode.value);
top.frames['two'].document.close();

But it doesn't work! Again the problem is with the javascript in the
page being pushed. In the main body of this document, there is an
inline call to a js function which is defined in an external file
referenced in the HEAD. I get an "object expected" error meaning that
it can't find the function called on that line.

The first test page I made is at:
www.hikksworld.com/frames/frameset1.html

It uses a text area to get the code, and document.write's it to the
right hand frame. The code that is default in the textarea was the code
I was testing it with. It loads an external .js file and then executes
it onclick of the link it generates. So I am not sure why yours is not
working. The only major difference I see between mine and yours is that
I am not using .open('about:blank') but when I used it, it didn't seem
to make any difference.

I'm guessing that when a document is built using document.write() the
different parts of the page - inline js, external js etc - are
executed in a different order.

No. When the page is loaded, it is parsed in the order that you give it
unless you use a defer attribute (which I believe to be an IE-only
attribute, not sure because I never use it).
Funnily enough, when I refresh the page that has been pushed, it WORKS
ABSOLUTELY FINE! So obviously the browser has built the page in the
right order on refresh.

Oh yeah: I tried splitting the source code into lines (using "split"
on newlines), and then writing out each line separately. I thought if
I wrote each line separately, it might affect the "parse order".

IE crashed after a few lines :(

Splitting the source shouldn't matter, but it crashing IE could be a
side effect of it since its introducing more and more calls to
document.write. BTW, what version IE are you using?
Thanks for your other suggestions, too, but I feel that if I could get
either the document.location/innerHTML or document.write() models
working, that would be the best course.

Any further suggestions?

Are frames a requirement? It would be easier to "push" innerHTML into a
div tag and load the external .js files in the main page. Thereby
eliminating the problem. You could try that approach with the frames but
the external files would have to be re-written to handle the
cross-frames issues.
 
W

Woody

hmm, i built page in js, worked great withteh includes then put it into
asp.. same problem as you, so i hardcopied teh included stuff into teh
page and now it works


Woody
any sugestion or comment made by me should be examined first for
validity and appropriateness before assuming i have any idea at all
what the heck i am talking about. I am not responsible for anything you
may see with my name attached to it, i think.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top