使Javascript代码部署就绪

使Javascript代码部署就绪,javascript,jquery,Javascript,Jquery,我需要让代码部署就绪。我无法硬编码这些URL,但由于某种原因,任何其他编码方式都会中断。请在此处提及此问题: 我基本上需要让开关检查位置是否包含“settings/”后面的页面名称,即iframe-home.php、update.php或changepassword.php。我想这就是我解决这个问题的方法?但我不知道怎么做。(希望有道理) 代码如下: $(document).ready(function() { $('iframe#settings-iframe').on('load',

我需要让代码部署就绪。我无法硬编码这些URL,但由于某种原因,任何其他编码方式都会中断。请在此处提及此问题:

我基本上需要让开关检查位置是否包含“settings/”后面的页面名称,即iframe-home.php、update.php或changepassword.php。我想这就是我解决这个问题的方法?但我不知道怎么做。(希望有道理)

代码如下:

$(document).ready(function() {
    $('iframe#settings-iframe').on('load', function() {
    var location = this.contentWindow.location.href;
    console.log('location : ', location);
    switch (location) {
        case "http://localhost/Makoto/profile/settings/iframe-home.php":
        console.log(location);
        activateHome();
        break;
      case "http://localhost/Makoto/profile/settings/changepassword.php":
        console.log(location);
        activatePassword();
        break;
      case "http://localhost/Makoto/profile/settings/update.php":
        console.log(location);
        activateName();
        break;
    }
  });
});
注意,我假设您希望动态检查没有主机部件的路径

创建新的链接元素,将
href
设置为
位置
,将其与
路径名

//替换为this.contentWindow.location.href
变量url=”http://localhost/Makoto/profile/settings/iframe-home.php";
/**
* http://localhost/Makoto/profile/settings/iframe-home.php
*将返回/Makoto/profile/settings/iframe-home.php
*/
变量链接=$(''{
href:url
})[0]。路径名;
var parts=link.split('/');
var file=parts[parts.length-1];
console.log(文件);
开关(文件){
案例“iframe home.php”:
激活home();
打破
案例“changepassword.php”:
激活密码();
打破
案例“update.php”:
活化胺();
打破
}
函数activateHome(){console.log('Activating home');}
函数activatePassword(){console.log('Activating password');}
函数activateName(){console.log('Activating name');}

将不同的代码添加到每个页面,而不是将其添加到所有页面并只执行其中的一部分,不是更容易吗?这样,如果您在update.php-page上,就可以执行代码,而不必担心需要执行什么代码。@Esko如果
iframe
有动态URL怎么办?