2012-07-12 282 views
0

我想知道如何將JavaScript文件中的變量值傳遞到寫入HTML文件頂部的腳本函數中的變量。將變量值傳遞給腳本函數中的變量(Javascript)

我寫的是這樣的:

myjsfile.js

var abc = "10"; 

HTML文件

<html> 
<head> 
<script type="text/javascript"> 
(function() { 

var test = document.createElement('script'); test.type = 'text/javascript'; test.async = true; 

test.src = 'testquery.js'; 

var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(test, s); 

alert(abc); 
})(); 

</script> 
</head> 
<body> 
</body> 
</html> 

我沒有得到的輸出。請幫忙!提前致謝。

基本上我試圖創建一個jquery插件就像谷歌分析。

回答

0
<html> 
    <head> 
    <script type="text/javascript" src="testquery.js"></script> 
    <script type="text/javascript"> 
     alert(abc); 
    </script> 
</head> 
<body></body> 
</html> 
+0

test.onload = function(){alert(abc);}代碼爲我工作....但如何顯示圖像存儲在外部jquery – Christina 2012-07-12 07:07:56

2

該腳本首先加載,請嘗試使用onload

test.onload=function(){alert(abc);} 
+0

非常感謝它的工作。我可以知道如何使用外部JavaScript文件顯示圖像。這是我想要顯示從jquery文件調用的圖像,而不是變量。 – Christina 2012-07-12 07:04:37

0

試試這個:

(function() { 

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

    test.onload = function() { 
     alert(abc); 
    } 

})();​ 

以及..在onload已經被告知,但你至少應該切換您的將其附加到頭部的方式。