Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何显示javascript源文件的内容?_Javascript - Fatal编程技术网

如何显示javascript源文件的内容?

如何显示javascript源文件的内容?,javascript,Javascript,如何显示javascript源文件的内容?我有源文件的url。我只想在文本区域和警报框中显示它。不管文件大小 <pre>function getFile() { var url = "http://example.com/s/js/file.js"; if(window.XMLHttpRequest) { //code for IE7+, FF, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } els

如何显示javascript源文件的内容?我有源文件的url。我只想在文本区域和警报框中显示它。不管文件大小

<pre>function getFile() {  
  var url = "http://example.com/s/js/file.js";
if(window.XMLHttpRequest)
{
    //code for IE7+, FF, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{
    //code for IE5,IE6
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function redraw()
{
    if(xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        alert(xmlhttp.responseText);
    }
}
xmlhttp.open("GET",url,false);
xmlhttp.send();
}
</pre>
函数getFile(){
变量url=”http://example.com/s/js/file.js";
if(window.XMLHttpRequest)
{
//IE7+、FF、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{
//IE5、IE6的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数redraw()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
警报(xmlhttp.responseText);
}
}
open(“GET”,url,false);
xmlhttp.send();
}

您可以这样做:


您正在寻找AJAX。如果文件位于同一个域中,则可以使用AJAX获取其文本。如果没有,您将需要一个服务器端代理脚本来返回文本。您可以通过ajax请求获取它,或者在创建页面之前在服务器端预加载它。实际上,我的期望是,我有一个服务器端js文件。我需要从那个文件中获取一些信息。我有url,即js文件在服务器中的位置。当我在浏览器中点击url时,就会得到响应。但当我尝试上面的代码时,它不起作用
$.get("file.js", function(file) {
    $("textarea").val(file);
});