By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

trouble inserting JavaScript string into page without eval()

P: n/a
Hi,

I have tried the following based on suggestions of the best way to
insert JavaScript into a page. This is instead of using eval().

Unfortunately IE says "unexpected call to property or method access"
for the second to last line of my function.

If you know what I've done wrong or how to fix it I would appreciate
the help.

Thank you,
Peter
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert Test</title>

<script type="text/javascript">

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
document.getElementById("myDiv").appendChild(newSc ript);
}

</script>

</head>
<body onload="insertScript();">

<div id="myDiv">
stuff
</div>

</body>
</html>

Oct 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Oct 16, 6:42 pm, petermich...@gmail.com wrote:
Hi,

I have tried the following based on suggestions of the best way to
insert JavaScript into a page. This is instead of using eval().
Who suggested doing it the way you have it written? It wasn't me,
that's for sure.
Unfortunately IE says "unexpected call to property or method access"
for the second to last line of my function.

If you know what I've done wrong or how to fix it I would appreciate
the help.
First, is this the third or fourth time you have asked this question in
different forms?
Second, why are you making it harder than it has to be?

<snip>

Ditch these two lines:
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
And do it the way I have always written it for you to do:

newScript.text = "alert('hi')";

Meaning, simply set the script element's .text property with the string
you want to use for the body of the script block.

--
Randy
I hate Google Groups

Oct 17 '06 #2

P: n/a
One Dumm Hikk wrote:
On Oct 16, 6:42 pm, petermich...@gmail.com wrote:
First, is this the third or fourth time you have asked this question in
different forms?
Second, why are you making it harder than it has to be?
Because I remember having troubles with this when I tried is previously
and repeating the experiments yesterday means I needed to revisit this.
Safari is the problem.

Ditch these two lines:
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line

And do it the way I have always written it for you to do:

newScript.text = "alert('hi')";

Meaning, simply set the script element's .text property with the string
you want to use for the body of the script block.
This does work in Firefox, Opera and IE but does not seem to work in
Safari.

Peter

Oct 17 '06 #3

P: n/a

pe**********@gmail.com wrote:
One Dumm Hikk wrote:
On Oct 16, 6:42 pm, petermich...@gmail.com wrote:
First, is this the third or fourth time you have asked this question in
different forms?
Second, why are you making it harder than it has to be?

Because I remember having troubles with this when I tried is previously
and repeating the experiments yesterday means I needed to revisit this.
Safari is the problem.

Ditch these two lines:
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
And do it the way I have always written it for you to do:

newScript.text = "alert('hi')";

Meaning, simply set the script element's .text property with the string
you want to use for the body of the script block.

This does work in Firefox, Opera and IE but does not seem to work in
Safari.
Any ideas about solving this problem?

Thank you,
Peter

Oct 18 '06 #4

P: n/a
Peter Michaux said the following on 10/18/2006 2:42 PM:
pe**********@gmail.com wrote:
>One Dumm Hikk wrote:
>>On Oct 16, 6:42 pm, petermich...@gmail.com wrote:
First, is this the third or fourth time you have asked this question in
different forms?
Second, why are you making it harder than it has to be?
Because I remember having troubles with this when I tried is previously
and repeating the experiments yesterday means I needed to revisit this.
Safari is the problem.
Ahh, never used Safari (no MAC here) so I can't test it.
>>
>>Ditch these two lines:

var s = document.createTextNode("alert('hi');");
Try adding an alert here to see what s is.
>>> newScript.appendChild(s); // problem line
And do it the way I have always written it for you to do:

newScript.text = "alert('hi')";

Meaning, simply set the script element's .text property with the string
you want to use for the body of the script block.
This does work in Firefox, Opera and IE but does not seem to work in
Safari.

Any ideas about solving this problem?
This is your original function:

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
document.getElementById("myDiv").appendChild(newSc ript);
}

You create a text node, then append it to the script element that hasn't
been appended to anything, that may be what Safari is balking on. Try
changing the order of your calls:

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
document.getElementById("myDiv").appendChild(newSc ript);
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
}

Then, the browser actually has an element to appendChild the text node
to. It may work, may not. I don't have a MAC to test it on. If it fails,
your best recourse for Safari testing may be RobG or either Richard
Cornford (They are the only two that I know for sure have access to a
MAC, invariably there are others here, I just don't know about them).

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Oct 18 '06 #5

P: n/a
Randy Webb wrote:
Peter Michaux said the following on 10/18/2006 2:42 PM:
pe**********@gmail.com wrote:
One Dumm Hikk wrote:
On Oct 16, 6:42 pm, petermich...@gmail.com wrote:
First, is this the third or fourth time you have asked this question in
different forms?
Second, why are you making it harder than it has to be?
Because I remember having troubles with this when I tried is previously
and repeating the experiments yesterday means I needed to revisit this.
Safari is the problem.

Ahh, never used Safari (no MAC here) so I can't test it.
>
Ditch these two lines:

var s = document.createTextNode("alert('hi');");

Try adding an alert here to see what s is.
>> newScript.appendChild(s); // problem line
And do it the way I have always written it for you to do:

newScript.text = "alert('hi')";

Meaning, simply set the script element's .text property with the string
you want to use for the body of the script block.
This does work in Firefox, Opera and IE but does not seem to work in
Safari.
Any ideas about solving this problem?

This is your original function:

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
document.getElementById("myDiv").appendChild(newSc ript);
}

You create a text node, then append it to the script element that hasn't
been appended to anything, that may be what Safari is balking on. Try
changing the order of your calls:

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
document.getElementById("myDiv").appendChild(newSc ript);
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
}
Same nothingness with this in Safari. I tried this variation and some
others also. Couldn't get anything working in Safari.

-----------

I have returned to eval() for this. However I am being careful with my
variables in the bit of JavaScript text so that these variables will be
in the same scope that your method would put them in.

So instead of trying to insert a script like this into the page using a
script element

var foo={};

I am using eval on a script like this

global.foo={};

where global was defined in the head element of the document how
Richard defines it "var global = this;" so that global is the window
object. I'm not sure why I don't just use "window.foo={};" but Richard
seems pretty keen on the global=this technique and probably for good
reason.

So now foo is not in the local scope of the insertScript() function but
foo is in the global scope.

Sorry my language here is a bit clunky. I hope that is clear. It does
seem to work.

-------

I suppose we could do a test insertion of a JavaScript variable with
your normal text property method. After the insertion we could see if
the variable exists. If it does not exist we could use the
createTextNode method that Safari seems to require.

I do think that the eval method I'm now using is less prone to browser
bug problems in the future.

Peter

Oct 18 '06 #6

P: n/a
ASM
Randy Webb a écrit :
I don't have a MAC to test it on. If it fails,
your best recourse for Safari testing may be RobG or either Richard
Cornford (They are the only two that I know for sure have access to a
MAC, invariably there are others here, I just don't know about them).
With my Safari (1.3.2)

that here :

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
document.getElementById("myDiv").appendChild(newSc ript);
}

works as it would do (and as waited, expected)

but that bellow :

function insertScript() {
var newScript = document.createElement('script');
newScript.type = "text/javascript";
document.getElementById("myDiv").appendChild(newSc ript);
var s = document.createTextNode("alert('hi');");
newScript.appendChild(s); // problem line
}

doesn't work

I don't understand this 2nd way can work (in FireFox it does):
I thought 'newsSript' since it has been appended in 'myDiv' was not more
known : it is now only some JavaScript script (no id, no name).
On my idea it was a virtual and temporary element/object.
And, supposing it is yet known, appended has run and finish.

Would that say I can insert in this new JavaScript what I want at any
moment later (except with Safari) ?
(newScript is definitively known as soon as inserScript() fires ?)

--
ASM
Oct 18 '06 #7

P: n/a
ASM
ASM a écrit :

2 ways to append javascript

on my Mac :
no one of two ways work with iCab
both work with Opera 9
Oct 18 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.