Page 1 of 1

External Javasctip document.write

Posted: Tue Jan 09, 2007 10:35 pm
by JellyFish
I have:

Code: Select all

<script type="text/javascript" src="MyJsFile.js"></script>
in the head of my html document. In the MyJsFile.js file I have a window.onload set to load() function and in that function I have:

Code: Select all

document.write("<img src='sdjfljslfj.gif' />");
I show this particular line because it acts like:

Code: Select all

document.open()
document.write("<img src='sdjfljslfj.gif' />");
Basically the image is only thing on the page when the window finishes loading.

So, is this because of the fact that it's in an external source? Why is this happening exactly?


Quick question: Is it possible to add script in between the script tags if the src attribute is set to an external js file? E.g:


Code: Select all

<script type="text/javascript" src="MyJsFile.js">
//place the onload event handle here
</script>
...Thanks for reading my post. :D

Posted: Tue Jan 09, 2007 11:47 pm
by feyd
It's best to use the DOM to generate the tag and insert it into the body.

Posted: Wed Jan 10, 2007 12:49 am
by JellyFish
I'm not familiar with what you're saying? Isn't using the write() method inserting a new element to the DOM?

Or are you saying that I should use the appendChild() method, or something, for the body?

Posted: Wed Jan 10, 2007 1:45 am
by Kieran Huggins
I'm not familiar with what you're saying? Isn't using the write() method inserting a new element to the DOM?
not always

Code: Select all

Or are you saying that I should use the appendChild() method, or something, for the body?
definitely!

Posted: Wed Jan 10, 2007 1:58 am
by JellyFish
I see what you mean now. Using createElement etc.

What I'm doing is taking my script in my document and trying to make it external. I'm not used to external scripting. So what I'd like to know is what would "window.document" reference to in the external script? Would it refer to the document of that's calling the external script?


Also, I used:

Code: Select all

ttDiv = document.createElement("div");
ttDiv.id = "MyDiv";
document.body.appendChild(ttDiv);
does this look right to you or would setAttribute be better then the id property?

Another question I have is: Say I have variable defined in a if statement in a function. Now where would that be accessible? I don't think that would be global, I'm assuming that it would only be available in that particular function. So how would I extent it's accessibility? I noticed that when I remove the "var" keyword from the line where I define the variable it makes it global, I think? Why when I remove the "var" keyword does it do that and why is there no error there is it because that it's no longer a variable but a reverence to an object or something?


Whew... Sorry for all the questions. But if you could answer as many of them as possible I'd very much appreciate it. I'm just a little bit stumped. 8O

Posted: Wed Jan 10, 2007 4:37 am
by Kieran Huggins
Don't be embarrassed at all - in the short time I've been here, these are some of the best javascript questions I've seen asked.

You're ready to benefit greatly from these two resources:

http://www.quirksmode.org/

- and -

http://javascript.crockford.com/

Generally, I always tend to go with the w3c spec... unless it's cuckoo. In this case, element.setAttribute() is the most correct way to go.

2/3rds of the way through this brilliant article your question about the confusing use of var is answered. (you're on the right track)

I don't have a clue what you mean by "external scripting", unless you mean the file is being included with the use of:

Code: Select all

<script src="someFile.js"></script>
which is perfectly normal. No script runs outside of the window's context.