Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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_Jquery - Fatal编程技术网

Javascript 使用主机名从地址运行文件

Javascript 使用主机名从地址运行文件,javascript,jquery,Javascript,Jquery,有一个脚本,我正试图使用的博客。当您将域URL输入到src中时,它会起作用,但我正在试图找到一种使用主机名插入域的方法 原文: <script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=INSERT-YOUR-URL-HERE&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_

有一个脚本,我正试图使用的博客。当您将域URL输入到src中时,它会起作用,但我正在试图找到一种使用主机名插入域的方法

原文:

<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=INSERT-YOUR-URL-HERE&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json" />
我试过:

<script type="text/javascript">
    var excuteTopCommentators = "http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"
    return excuteTopCommentators
</script>
我还尝试了document.write:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');
</script>

这两种尝试似乎都不起作用。关于如何在不手动将域URL插入脚本src的情况下执行此操作,您有什么想法吗?

也许问题在于您获得了脚本,但它从未执行过

也许可以试试jQuery的getScript


您的第二个选项看起来不错:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');
</script>
脚本已下载,但不执行任何操作。这是因为它是一个JSONP,而不是一个脚本

所以,您需要一个回调函数来让它工作:请注意,这是数据,而不是代码

查看查询,例如_callback=getYpipePP和_render=json


相关:

尝试此操作您遇到的问题是结束脚本标记:

<script type="text/javascript">
        document.write('<script language="javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'
                     + window.location.hostname
                     + '&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json" type="text/javascript"><\/script>')
    </script>

您在开发人员控制台中看到了什么?下载文件?我对你的第二次尝试很感兴趣。非法的返回语句和意外的标识符IDN不起作用,并且在chrome控制台中没有显示任何错误。请尝试直接在浏览器URL栏中打开脚本。你能排除脚本是否存在吗?当我手动输入我的域URL时,脚本工作。当我尝试使用hostname时,它不起作用。也许您应该检查window.location.hostname是否与静态域url相同。它可能缺少协议-如中所示http://perhaps !? 他们在说什么?纯粹的猜测。我已经做了研究,正确的答案是JSONP。不起作用:未捕获的语法错误:意外标识符。我怀疑这可能与这是一个XML模板这一事实有关。我试图将CDATA标记添加到脚本中,但出现了错误“非法标记”。@Xarcell其工作原理是,您获取的是JSONP对象,而不是脚本。查看plunker,查看获取的数据,只需调用一个名为getypipep的函数,并将一个JSON对象作为参数传递。只是jsonp有。对不起,它没有。输出为‘;当你的问题摆在眼前时,你却看不到问题的答案,这真是太遗憾了。
<script type="text/javascript">
    // JSONP Callback
    function getYpipePP(data) {
        alert(JSON.stringify(data));
    }

    document.write('<script type="text/javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'+window.location.hostname+'&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json"></script>');

</script>
<script type="text/javascript">
        document.write('<script language="javascript" src="http://pipes.yahoo.com/pipes/pipe.run?YourBlogUrl=http://'
                     + window.location.hostname
                     + '&amp;ShowHowMany=5&amp;_id=390e906036f48772b2ed4b5d837af4cd&amp;_callback=getYpipePP&amp;_render=json" type="text/javascript"><\/script>')
    </script>