Including external javascript code in a web page

D

David D.

In html, one can say <script language="JavaScript"
src="http://someCodeSnippet.js">

Is there any way to embed the included code snippet in a function (in the
case where it is not already a function in the remote souce file)?

- David D.
 
M

McKirahan

David D. said:
In html, one can say <script language="JavaScript"
src="http://someCodeSnippet.js">

Is there any way to embed the included code snippet in a function (in the
case where it is not already a function in the remote souce file)?

- David D.

More properly:

<script type="text/javascript" src="someCodeSnippet.js"></script>

or

<script type="text/javascript"
src="http://{domain}/{folder}/someCodeSnippet.js"></script>


Could you be more explicit regarding "embed the included code snippet in a
function"?
 
D

David D.

McKirahan said:
Could you be more explicit regarding "embed the included code snippet in a
function"?

Let's say the referenced file, from a news service, contains the following
two lines of code:

document.write('<br><a href="worldnews.com/article0391.html">New Casualties
in Iraq</a>');
document.write('<br><a href="worldnews.com/article0402.html">Palestine Peace
Overture</a>');

This code is meant to be copied, in-line, into your web page, using the
<script ... src="..."> syntax.

However, there is a timing problem. The first time the web page is brought
up after a fresh boot, the document.writes are not executed. Thereafter,
when the files are cached in RAM, the web page displays fine. (I have tried
it with I.E., Netscape, and Mozilla).

As a workaround, I want to try creating a function in the page header:

function getNews() {
document.write('<a href="worldnews.com/article0391.html">New Casualties
in Iraq</a>');
document.write('<a href="worldnews.com/article0402.html">Palestine Peace
Overture</a>');
}

In line, at the point in the page where I would want the links displayed, I
would call the function.

However, I have been unsuccessful in trying to create that function in the
header, using the dynamic links from the referenced file.

- David
 
R

RobB

David D. said:
Let's say the referenced file, from a news service, contains the following
two lines of code:

document.write('<br><a href="worldnews.com/article0391.html">New Casualties
in Iraq</a>');
document.write('<br><a href="worldnews.com/article0402.html">Palestine Peace
Overture</a>');

This code is meant to be copied, in-line, into your web page, using the
<script ... src="..."> syntax.

However, there is a timing problem. The first time the web page is brought
up after a fresh boot, the document.writes are not executed. Thereafter,
when the files are cached in RAM, the web page displays fine. (I have tried
it with I.E., Netscape, and Mozilla).

As a workaround, I want to try creating a function in the page header:

function getNews() {
document.write('<a href="worldnews.com/article0391.html">New Casualties
in Iraq</a>');
document.write('<a href="worldnews.com/article0402.html">Palestine Peace
Overture</a>');
}

In line, at the point in the page where I would want the links displayed, I
would call the function.

However, I have been unsuccessful in trying to create that function in the
header, using the dynamic links from the referenced file.

- David

Generally speaking, unlike HTML objects (like images, Flash, and
Java), which can be requested in parallel, the HTML parser must wait
for the JavaScript interpreter to load and execute any JavaScript
files before it can continue. So this shouldn't be an issue. Where are
you embedding the <script> tags?
 
D

David D.

The news links are in a table cell.

It is actually my brother's web site, www.somerton.net. I am trying to
help him debug the problem.

If you are able to reproduce the problem, then the first time you load the
page, you will see "World News" in the right hand column, but no news links.
If you click the "Refresh" button, then the news links will appear. The
problem will have gone away until your next cold boot.

- David D.
 
?

.

David said:
In html, one can say <script language="JavaScript"
src="http://someCodeSnippet.js">

Is there any way to embed the included code snippet in a function (in the
case where it is not already a function in the remote souce file)?

- David D.
This is the exact question I have. Unfortunately, perusing the billion
messages this list generates, it's been asked several times in the past,
with no answers. So I'm guessing: no.

EXPERTS! Please prove me wrong!




The closest I can come is something like::
document.write("<script src='stupid.js'><" + "/script>"); /* NOTE
the break is need to prevent it from closing your script prematurely.*/

This will not run the external js until after the </script> tag is
closed. But it will run it before any further html or script tags.

So, My situation is: I have a parsing <script> that parses out info
from the URL. it decides which external JS file to doc.write(). this
file does not load until the parsing script closes. Then! I open another
<script> which uses the variables from the external JS.

It's a mess. It's ugly. I'm not even sure if it will work 100%,(works in
IE6 and Moz1.4) but that's the closest I can get, short of learning PHP...

Phil

(e-mail address removed) (to email, the CAPITAL LETTERS)
 
R

Richard Cornford

.. wrote:
This is the exact question I have. Unfortunately, perusing
the billion messages this list generates, it's been asked
several times in the past, with no answers. So I'm guessing:
no.
<snip>

While it may not be possible to insert the code imported by an external
script in a function so that its execution can be deferred, it would be
possible to defer any document.write calls that such code may use.

First copy the real document.write mehtod to another property of the
document object (to preserve its - this - reference when executed):-

document.oldWrite = document.write;

Then create an array in a suitable containing scope (global here) to
contain anything written:-

var stuffThatWasWritten = [];

Then replace the document.write method with one that will just copy its
arguments to that array:-

document.write = function(){
for(var c = 0;c < arguments.length;c++){
stuffThatWasWritten[stuffThatWasWritten.length] = arguments[c];
}
}

Now import the external script and instead of writing to the document it
will just store strings in the array.

Then resort the document.write method (or use the copy of it) and write
out whatever is in the array at any point you choose:-

document.oldWrite(stuffThatWasWritten.join(''));

-or:-

document.write = document.oldWrite;
document.write(stuffThatWasWritten.join(''));

(but don't forget the document.writeln method)
The closest I can come is something like::
document.write("<script src='stupid.js'><" + "/script>");
/* NOTE the break is need to prevent
it from closing your script prematurely.*/

The concatenation is not needed. Only disrupting the '</' character
sequence in the source code is required and can be achieved by escaping
the forward slash, resulting in '<\/'. And avoiding the concatenation is
fractionally more efficient.

Richard.
 
?

.

Richard Cornford wrote:

Then replace the document.write method with one that will just copy its
arguments to that array:-

document.write = function(){
for(var c = 0;c < arguments.length;c++){
stuffThatWasWritten[stuffThatWasWritten.length] = arguments[c];
}
}

Huh. I didn't realise could overwrite some of the default functions.
That would realy confuse the heck out of me if I did that accidentally...

The concatenation is not needed. Only disrupting the '</' character
sequence in the source code is required and can be achieved by escaping
the forward slash, resulting in '<\/'. And avoiding the concatenation is
fractionally more efficient.

I really should have remembered the escape characters. I only play with
Javascript when I sporadically tweak my website, so I always seem to
have to relearn everything each time...
Thanks, hopefully this helped the original poster as well.

Phil
 
G

Grant Wagner

. said:
Richard Cornford wrote:

Then replace the document.write method with one that will just copy its
arguments to that array:-

document.write = function(){
for(var c = 0;c < arguments.length;c++){
stuffThatWasWritten[stuffThatWasWritten.length] = arguments[c];
}
}

Huh. I didn't realise could overwrite some of the default functions.
That would realy confuse the heck out of me if I did that accidentally...

And many people do do it accidentally:

<form name="myForm">
<input type="hidden" name="function" value="">
<input type="submit" name="submit" value="Save record">
<input type="button" name="delete" value="Delete record"
onclick="
this.form.elements['function'].value='delete';
this.form.submit(); /* why doesn't this work? */
">
</form>
 

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

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top