javascript - 我加载这个 js 数组时做错了什么?

标签 javascript php jquery arrays

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

<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 文件正在输出:

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 文件。

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

有什么想法吗?

谢谢!

最佳答案

您已经为该变量创建了一个本地作用域 - 您应该通过将 loadUrls.php 设置为简单的脚本(不带 $(document).php ),将其放入全局作用域。准备好包装器):

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

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

关于javascript - 我加载这个 js 数组时做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530364/

相关文章:

javascript - vuejs、fabricjs 和 window 的事件处理

javascript - 作为 props 传递的数据不会在 django 和 Reactjs 中显示

javascript - 空时隐藏动态填充的 Div -- Rails 4

javascript - jQuery 找到最近的 ul

javascript - CSS随机动画

javascript - 使用 JavaScript 的 `window.cookie` 持久化 CSS 状态

javascript - 连接两个页面内的数据

php - 回显一行输出或关闭 php 标签与写出 html 代码之间有什么区别吗?

php - 尝试初始化 JSONObject 时出现 Android ResponseListener JSON 错误

php - Twitter 正则表达式只有在还没有链接时