使JavaScript代码准备就绪



我需要准备好此代码。我不能将这些URL的编码硬编码,但是由于某种原因,任何其他对此进行编码的方法都会破裂。在此处引用此问题:在iframe jQuery中的表单提交重定向时,未检测到更新的SRC属性

我基本上需要让开关检查位置是否包含'settings/''的页面名称,即iframe-home.php,update.php或thackpassword.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;
    }
  });
});

note 我假设您需要动态检查没有主机零件的路径。

创建新的链接元素,将href设置为location,将其与pathname

进行比较

// replace with this.contentWindow.location.href
var url = "http://localhost/Makoto/profile/settings/iframe-home.php";
/**
 * http://localhost/Makoto/profile/settings/iframe-home.php
 * will return /Makoto/profile/settings/iframe-home.php
 */
var link = $('<a>', {
  href: url
})[0].pathname;
var parts = link.split('/');
var file = parts[parts.length - 1];
console.log(file);
switch (file) {
  case "iframe-home.php":
    activateHome();
    break;
  case "changepassword.php":
    activatePassword();
    break;
  case "update.php":
    activateName();
    break;
}
function activateHome() {console.log('Activating home');}
function activatePassword() {console.log('Activating password');}
function activateName() {console.log('Activating name');}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

最新更新