2017-06-27 6 views
0

Ich muss diesen Code bereitstellen bereitstellen bereit. Ich kann diese URLs nicht hart codieren, aber aus irgendeinem Grund bricht jede andere Art der Codierung ab. Verweisen Sie diese Frage hier: Upon Redirect of Form Submission within iFrame jQuery Not Detecting Updated Src AttributeBereitstellung von Javascript-Code Bereit

Ich muss im Grunde die Schalter überprüfen, ob der Speicherort enthält den Seitennamen nach 'Einstellungen /', d. H. Iframe-home.php, update.php, oder changepassword.php. Ich denke, das ist, wie ich dieses Problem behebe? Aber ich bin mir nicht sicher wie. (Ich hoffe, das macht Sinn)

Hier ist der Code:

$(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; 
    } 
    }); 
}); 
+0

Wäre es nicht viel einfacher sein, verschiedene Code stattdessen zu jeder Seite hinzufügen davon alle Hinzufügen Seiten und nur etwas davon ausführen? Wenn Sie sich auf der update.php-Seite befinden, können Sie den Code einfach ausführen und sich nicht darum kümmern, welcher Code ausgeführt werden soll. – Esko

+0

@Esko Was ist, wenn 'iframe' dynamische URLs hat? – Justinas

Antwort

0

Hinweis Ich gehe davon aus, dass Sie dynamisch Pfadteil ohne Host überprüfen möchten.

Neues Link-Element, setzen href zu location, vergleichen Sie es mit 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>