trouble inserting JavaScript string into page without eval()

P

petermichaux

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(newScript);
}

</script>

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

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

</body>
</html>
 
O

One Dumm Hikk

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

petermichaux

One said:
On Oct 16, 6:42 pm, (e-mail address removed) 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:


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
 
P

Peter Michaux

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.



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
 
R

Randy Webb

Peter Michaux said the following on 10/18/2006 2:42 PM:
Ahh, never used Safari (no MAC here) so I can't test it.

Try adding an alert here to see what s is.
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(newScript);
}

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(newScript);
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).
 
P

Peter Michaux

Randy said:
Peter Michaux said the following on 10/18/2006 2:42 PM:

Ahh, never used Safari (no MAC here) so I can't test it.


Try adding an alert here to see what s is.


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(newScript);
}

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(newScript);
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
 
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(newScript);
}

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(newScript);
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 ?)
 
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
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top