加载此js数组时我做错了什么? [英] What am I doing wrong loading this js array?

查看:104
本文介绍了加载此js数组时我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经添加了一个关于我认为这里存在问题的问题,但是我认为更多细节会有所帮助.所以,这是我的代码:

I've added another question specifically about what I think is the problem here, but I think a bit more detail will do some good. So, this is my code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Prueba jQuery no event</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="loadUrls.php"></script>
<script type="text/javascript">
jQuery(document).ready(function() { 

    jQuery("a").click(function(ev) {

        //ev.preventDefault();

        // Defino las variables
        var currentAnchor = jQuery(this);
        var currentHref = currentAnchor.attr('href');
        var curHref = currentHref.split('/');
        var curHrefFinal = curHref[2].replace('www.', '');

        if(jQuery.inArray(curHrefFinal,urlsFinal) > -1) {
            // Evito que se visite el link directo
            ev.preventDefault();

            if (ev.metaKey || ev.ctrlKey) {
                // Redirecciono el navegador a la página que queremos
                window.open(currentHref + "?a=esta-funcionando-tambien");
            } else {
                // Redirecciono el navegador a la página que queremos
                window.location = currentHref + "?a=esta-funcionando";
            };
        };
    });
});
</script>
</head>

<body>
<p>Hola, <a href="http://www.avantrip.com">este es</a> un link.</p>
<p>Hola, <a href="http://espana.aula365.com/es/">este es</a> otro link.</p>
<p>Hola, <a href="http://stackoverflow.com">este es</a> otro link.</p>
<p>Hola, <a href="http://www.airborn.com.ar">este es</a> otro link.</p>
</body>
</html>

loadUrls.php文件正在输出:

The loadUrls.php file is outputting:

jQuery(document).ready(function() { 
    var urlsFinal = [
        "avantrip.com.ar",
        "avantrip.com",
        "espana.aula365.com",
        "almashopping.com",
        "airborn.com.ar",
        "1and1.mx",
        "oiasdoiajsdoiasdoiasjdioajsiodjaosdjiaoi.com"
    ];
});

基本上,我将所有被单击的链接的href属性与从SQL数据库动态生成的数组进行比较.该数组需要托管在一个外部文件中,因此必须在loadUrls.php文件中.

Basically, I am comparing the href attributes of all links being clicked to a array that is being generated dynamically from an SQL database. This array needs to be hosted in an external file, hence the loadUrls.php file.

由于某种原因,在主代码中无法识别urlsFinal var.如果我复制数组并将其粘贴到主文件中,则其余的工作正常.我找不到任何问题,但是我相信你会的.

For some reason, the urlsFinal var is not being recognised in the main code. If I copy the array and paste it in the main file, the rest of the thing is working correctly. I can't find any problems, but I'm sure you will.

有什么想法吗?

谢谢!

推荐答案

您已经为变量创建了本地作用域-您应该通过简单地将loadUrls.php编写为脚本(而不使用$(document).ready包装器):

You've created a local scope for the variable -- you should put it into the global scope, by making loadUrls.php simply the script (without the $(document).ready wrapper):

var urlsFinal = [
    "avantrip.com.ar",
    "avantrip.com",
    "espana.aula365.com",
    "almashopping.com",
    "airborn.com.ar",
    "1and1.mx",
    "oiasdoiajsdoiasdoiasjdioajsiodjaosdjiaoi.com"
];

将其添加到全局范围的另一种方法(如果说由于其他原因需要$(document).ready闭包)是使用window.urlsFinal = [ ... ].

An alternative way of adding it to the global scope (if say you needed the $(document).ready closure for some other reason), is to use window.urlsFinal = [ ... ].

这篇关于加载此js数组时我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆