Trang web này sẽ có chức năng hạn chế trong khi chúng tôi trải qua bảo trì để cải thiện trải nghiệm của bạn. Nếu một bài viết không giải quyết được vấn đề của bạn và bạn muốn đặt câu hỏi, chúng tôi có cộng đồng hỗ trợ của chúng tôi đang chờ để giúp bạn tại @FirefoxSupport trên Twitter và /r/firefox trên Reddit.

Tìm kiếm hỗ trợ

Tránh các lừa đảo về hỗ trợ. Chúng tôi sẽ không bao giờ yêu cầu bạn gọi hoặc nhắn tin đến số điện thoại hoặc chia sẻ thông tin cá nhân. Vui lòng báo cáo hoạt động đáng ngờ bằng cách sử dụng tùy chọn "Báo cáo lạm dụng".

Tìm hiểu thêm

Changing the src attribute for the script tag dynamically doesn't work as of Firefox 4.0

  • 6 trả lời
  • 2 gặp vấn đề này
  • 10 lượt xem
  • Trả lời mới nhất được viết bởi WD_2012

more options

Hello guys,

changing the src attribute for the script tag dynamically by the use of JavaScript was working in Firefox 3.. and ist still working in IE, but it is not working in the new versions of Firefox anymore.

Following example shows the problem:

test.html

html
  head
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
 <script type="text/javascript" src="fcts.js"></script>
 <script type="text/javascript" id="myscriptObject"></script>
  /head
  body
     <script> change_src() </script>
     <script> test() </script>
  /body
/html


fcts.js

function change_src(){

document.getElementById("myscriptObject").src = "fcts2.js";

}


fcts2.js

function test(){

alert("Hello");

}


The error console says: 'ReferenceError: test is not defined'

I have also tried to create the script element dynamically:

function change_src(){

 var script = document.createElement("script");
 script.type = "text/javascript";
 script.src = "fcts2.js";
 document.getElementsByTagName("head")[0].appendChild(script);

}

The result has been the same.

Thanks in advance for your help.

Hello guys, changing the src attribute for the script tag dynamically by the use of JavaScript was working in Firefox 3.. and ist still working in IE, but it is not working in the new versions of Firefox anymore. Following example shows the problem: '''test.html''' html head <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript" src="fcts.js"></script> <script type="text/javascript" id="myscriptObject"></script> /head body <script> change_src() </script> <script> test() </script> /body /html '''fcts.js''' function change_src(){ document.getElementById("myscriptObject").src = "fcts2.js"; } '''fcts2.js''' function test(){ alert("Hello"); } The error console says: 'ReferenceError: test is not defined' I have also tried to create the script element dynamically: function change_src(){ var script = document.createElement("script"); script.type = "text/javascript"; script.src = "fcts2.js"; document.getElementsByTagName("head")[0].appendChild(script); } The result has been the same. Thanks in advance for your help.

Được chỉnh sửa bởi WD_2012 vào

Tất cả các câu trả lời (6)

more options

Can you gain any insight from Firefox's error console (Ctrl+Shift+j)?

Try using a full URL when you set src and see whether that helps.

more options

It is usually better to either remove and reinsert the tag or replace the tag instead of modifying the source attribute.
I need do that manually in the DOM Inspector if I want to check embedded JavaScript on web pages that aren't working properly in Firefox. CSS changes work, but JavaScript changes are cached and can't be changed that way.

more options

Hello guys,

thanks for your replies.

Using the full URL doesn't help.

The Firefox's error console shows only one message: 'ReferenceError: test is not defined'.

Replacing the whole node has the same effect as changing the src property. In both cases the script object is shown with the new src value by the DOM Inspector. It seems like the src value is changed but the new file (fcts2.js) is not imported.

Furthermore I have found that the function test() is executed if I add a call of the alert function directly after changing the src value:


fcts.js

function change_src(){

document.getElementById("myscriptObject").src = "fcts2.js";

alert("OK");

}


Doing so, I get two dialogs - the first one is showing 'OK' and the second one is showing 'Hello'.

Any ideas?

Được chỉnh sửa bởi WD_2012 vào

more options

Does it work if you make the src change via a setTimeout() to make sure that the DOM has been finalized?

more options

Unfortunately, it doesn't help. I have also tried to set a timeout for the call of the function test(), but the result has been the same.

more options

May it be a bug in Firefox?