473,386 Members | 1,886 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,386 software developers and data experts.

Including external javascript code in a web page

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.


Jul 23 '05 #1
8 2555
"David D." <da*****************************@comcast.net> wrote in message
news:_t********************@comcast.com...
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"?
Jul 23 '05 #2
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:JlMqd.688747$8_6.455375@attbi_s04...

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

Jul 23 '05 #3
"David D." <da*****************************@comcast.net> wrote in message news:<Ks********************@comcast.com>...
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:JlMqd.688747$8_6.455375@attbi_s04...

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


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?
Jul 23 '05 #4
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.

"RobB" <fe******@hotmail.com> wrote in message
news:ab**************************@posting.google.c om...
"David D." <da*****************************@comcast.net> wrote in message

news:<Ks********************@comcast.com>...
"McKirahan" <Ne**@McKirahan.com> wrote in message
news:JlMqd.688747$8_6.455375@attbi_s04...

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


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?

Jul 23 '05 #5
.
David D. wrote:
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

sp********@happyINVALIDmagpie.com (to email, the CAPITAL LETTERS)


Jul 23 '05 #6
.. wrote:
<snip>
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)?
<snip> 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.
Jul 23 '05 #7
.
Richard Cornford wrote:

<snip>

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...

<snip>
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.


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...
Richard.

Thanks, hopefully this helped the original poster as well.

Phil

Jul 23 '05 #8
"." wrote:
Richard Cornford wrote:

<snip>

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>

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Theo | last post by:
Just a quick question... When including a page inside another (include('page.php')), is it bad for the second document to also have <head> and <body> tags etc? Im using iframes currently, and it...
5
by: joaopedrogoncalves | last post by:
Hi, I want to load an external javascript file, get its results and stick them inside a <div> block. I also want to do this in several places on a web page. This way the browser doesn't have...
4
by: Prowler | last post by:
In the application we are currently building, we need to write positioning code on-the-fly, based upon the screen offset of the element in the AS/400 application which drives the Web app. The 400,...
1
by: mb | last post by:
Hi, I picked up this JavaScript code (quiz code) at: http://javascript.internet.com/miscellaneous/multiple-choice-quiz.html and modified it (and may have deleted some line I shouldn't have) so...
5
by: Newbie Coder | last post by:
Hello all I have 3 functions in a javacrip file (MyScript.js), which is added to an ASP.NET 2.0 project 1) no right-click 2) no select text (copy...) 3) History.Back()' How do I call...
4
by: fmaxwell | last post by:
Dear Group I have a very frustrating problem. I have been trying to make it more difficult to access external javascript files by using PHP sessions. This works beautifully locally (both in...
38
by: Neo Geshel | last post by:
I am seeking a method to load one JS file directly into another, *without* having to dynamically write <scripttags. Is there any method whereby I can call only one external JS file using a ...
5
by: althafexcel | last post by:
hi everyone Im trying to include an external js in my aspx page under the head tag, it doesn't load or it displays an object expected error whenver the function from the .js is called. Actually...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.