473,396 Members | 2,036 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,396 software developers and data experts.

trouble inserting JavaScript string into page without eval()

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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.