使Javascript代码部署准备就绪

时间:2017-06-27 05:40:32

标签: javascript jquery

我需要准备好此代码部署。我无法对这些网址进行硬编码,但出于某种原因,任何其他编码方式,都会中断。在此处引用此问题:Upon Redirect of Form Submission within iFrame jQuery Not Detecting Updated Src Attribute

我基本上需要让交换机检查位置是否包含' 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;
    }
  });
});

1 个答案:

答案 0 :(得分:0)

注意我假设你想要在没有主机部分的情况下动态检查路径。

创建新的链接元素,将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>