简体   繁体   English

在ajax中运行javascript外部链接甚至内部

[英]run javascript external link and even internal in ajax

How many ways are to excute javascrpit code in ajaxed page ? 在ajaxed页面中执行javascrpit代码有几种方法? internal and externally ... I really need to execute my scripts but found no way to my goal I tried eval in a loop & getscript method etc ... but no result is there a real way to execute my scripts ? 内部和外部...我真的需要执行我的脚本,但是找不到实现我的目标的方法,我在循环和getscript方法等中尝试了eval ...但是没有结果可以执行我的脚本吗? I'm using load function to load my page please help me 我正在使用加载功能加载页面,请帮助我
I have google map it work in usual way but when I use ajax to load it, it won't work here's my code 我有谷歌地图它以通常的方式工作,但当我使用Ajax加载它时,它将无法正常工作这是我的代码

$('ul li a').on("click",function() {
    var link = $(this).attr("href") + " #contentx";
    $('#contentx').fadeOut('slow', function(){
        $('#contentx').load(link, function(){     
            $("#mapjs").each(function(i) {
                eval($(this).text());
            });
            $('#contentx').fadeIn('slow');
        });
    });
    return false;
});

i tried eval in a loop This really should work, could you post the code that you used? 我在一个循环中尝试了eval这确实应该工作,您可以发布使用的代码吗?

You can run javascript from another source with the eval() function. 您可以使用eval()函数从其他来源运行javascript。

ie: 即:

eval("alert(\"hello world\")");

Would show a message hello world. 将向世界显示消息。

You'll need to move your JS block ( <script id="mapjs">...</script> ) into its own dedicated file. 您需要将JS块( <script id="mapjs">...</script> )移到其自己的专用文件中。

If you're using $.load with a landing page fragment (ie a URL followed by #id), then any script tags will be removed, and not executed. 如果您将$.load与登录页面片段(即URL后接#id)一起使用,则所有脚本标签都将被删除,并且不会执行。 See the Script Execution section of the $.load documentation . 请参阅$ .load文档的“脚本执行”部分。

Psuedo-code - the 'proper' way would be be something like this: 伪代码-“正确”的方式如下所示:

$('ul li a').on("click",function() {
    var link = $(this).attr("href") + " #contentx";
    $('#contentx').fadeOut('slow', function(){
        $('#contentx').load(link, function(){
            if($(this).attr('href') == 'http://wearesevil.com/?page_id=16'){     
                $.getScript('/map.js', function(){
                    $("#mapjs").each(function(i) {
                        eval($(this).text());
                    });
                });
            }
            $('#contentx').fadeIn('slow');
        });
    });
    return false;
});

The 'hacky' way? “骇客”的方式? You could change your <script id="mapjs"> to be <div id="mapjs" style="display:none;">...</div> . 您可以将<script id="mapjs">更改为<div id="mapjs" style="display:none;">...</div> This wouldn't then be removed from the DOM, and you could use eval() on the text. 这样就不会从DOM中删除它,并且您可以在文本上使用eval()

I would say that you problem is caused by the same origin policy 我想说您的问题是由相同的原产地政策引起的

It is even mentioned in the documentation for $.load 它甚至在$ .load的文档中提到

You may simply check debug console in Firefox or Chrome for related error message. 您可以简单地在Firefox或Chrome浏览器中检查调试控制台以获取相关的错误消息。

For example in Firefox there will be reported something like: 例如,在Firefox中,将报告如下内容:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://maps.google.com . 跨域请求被阻止:同源策略禁止在https://maps.google.com上读取远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing). (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM