Safari doesn't execute <script> tags added using the DOM

M

MPennig

Here's my situation. I have a function defined in my document's <head>
section which adds a <script> tag to a specified <div> tag. The added
<script> has a src="" attribute pointing to a PHP file, which
dynamically returns JavaScript. In Safari, the src file isn't
downloaded. Neither is any code contained within the new script tag
[such as alert()]. In IE(6) and Firefox, this works as expected.
Here's my code. I hope it will better illustrate what I'm trying to
accomplish:
-------------------
[usage]

<div id="123456">
<a href="#" onclick="add_detail('123456')">Click me</a>
</div>

[in <head>]

function add_detail(divID) {
detail = document.getElementById(divID);
new_script = document.createElement('script');
new_script.setAttribute('src', 'get_details.php?id='+divID);
document.getElementById(occID).appendChild(new_script);
}

[get_details.php]
(returns dynamically generated javascript code for inserting text
through the DOM... is dependant on the 'id' variable in query string)
-------------------
My goal with this code is to selectively, dynamically receive text and
add it to the document tree without leaving or refreshing the page. I
chose not to load all of the text because it uses web services and
would be a heavy load, as each page has about 50-100 of the above
'Click me' links.

Either a solution to make this work in Safari, or a new method would
be greatly appreciated. Thanks in advance!
 
M

Mick White

MPennig said:
Here's my situation. I have a function defined in my document's <head>
section which adds a <script> tag to a specified <div> tag. The added
<script> has a src="" attribute pointing to a PHP file, which
dynamically returns JavaScript. In Safari, the src file isn't
downloaded. Neither is any code contained within the new script tag
[such as alert()]. In IE(6) and Firefox, this works as expected.
Here's my code. I hope it will better illustrate what I'm trying to
accomplish:
-------------------
[usage]

<div id="123456">
<a href="#" onclick="add_detail('123456')">Click me</a>
</div>

[in <head>]

function add_detail(divID) {
detail = document.getElementById(divID);
new_script = document.createElement('script');
new_script.setAttribute('src', 'get_details.php?id='+divID);

Why not:
new_script.src="get_details.php?id="+divID;
?
Mick
document.getElementById(occID).appendChild(new_script);
}

[get_details.php]
(returns dynamically generated javascript code for inserting text
through the DOM... is dependant on the 'id' variable in query string)
-------------------
My goal with this code is to selectively, dynamically receive text and
add it to the document tree without leaving or refreshing the page. I
chose not to load all of the text because it uses web services and
would be a heavy load, as each page has about 50-100 of the above
'Click me' links.

Either a solution to make this work in Safari, or a new method would
be greatly appreciated. Thanks in advance!
 
P

pennig

What's the difference between doing that and using the DOM function?
Whatever the case, I tried both just now, and there was no change.
Safari is still broken.
 
J

Jim Ley

What's the difference between doing that and using the DOM function?
Whatever the case, I tried both just now, and there was no change.
Safari is still broken.

Actually not, the HTML specification is completely underspecified in
this area, so it has no requirement to execute scripts in that
situation, it's quite a tricky thing to specify, especially once you
introduce the defer attribute.

Anyway, I would advise creating an IFRAME, which is much more
reliable, and have the scripts in that add themselves to the parent
windows scope. I'd only do any of this if you really, really need to
do script loading, is there really a sound reason too?

Jim.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top