469,356 Members | 2,013 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

innerHTML and variable scope

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!
Jul 23 '05 #1
4 2379
Nick wrote:

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

<--snip-->
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.
<--snip-->
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.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #2
> 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
Jul 23 '05 #3
Nick wrote:
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.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Jul 23 '05 #4
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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Keiron Waites | last post: by
23 posts views Thread by Russ Chinoy | last post: by
17 posts views Thread by PJ | last post: by
pbmods
1 post views Thread by pbmods | last post: by
8 posts views Thread by Pratik Patel | last post: by
112 posts views Thread by istillshine | last post: by
6 posts views Thread by PaPa | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.