473,396 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2594
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Keiron Waites | last post by:
Is there any way to use a variable.innerHTML = ""; instead of text.innerHTML = "";? It doesn't seem to work for me. Thanks, Keiron
6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
17
by: PJ | last post by:
Greetings... I have stumbled upon a small problem. I use Ajax to retrieve part of a page I need to update. I update a DIV element with the HTML contents I get from another page. It works...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
8
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
6
by: PaPa | last post by:
I'm not sure this is a javascript issue or an HTML issue. I notice that when I extract the contents of a div using the innerHTML property (?), that I wind up with a literal variable (?) which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.