javascript 从 window.location 设置 iFrame src

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12615055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:40:18  来源:igfitidea点击:

Set iFrame src from window.location

javascriptjqueryurl

提问by user1410949

Using jQuery or javascript, I have fumbled with this and finally give in.

使用 jQuery 或 javascript,我摸索着这个,最后屈服了。

How can I set an iframe src like this:

如何设置 iframe src 像这样:

  1. If the window URL contains a parameter starting with "?sku="
  2. Cache entire parameter from ? to end (?sku=T235N&addressline=90803&form=product_search)
  3. Change iframe src from /index.html to /default.html + parameter?

    Change iframe src="/index.html to src="/default.html?sku=T235N&addressline=90803&form=product_search"

  1. 如果窗口 URL 包含以“?sku=”开头的参数
  2. 从 ? 缓存整个参数 结束 (?sku=T235N&addressline=90803&form=product_search)
  3. 将 iframe src 从 /index.html 更改为 /default.html + 参数?

    将 iframe src="/index.html 改为 src="/default.html?sku=T235N&addressline=90803&form=product_search"

http://jsfiddle.net/Digitalctn/rUnQd/

http://jsfiddle.net/Digitalctn/rUnQd/

Thank you a ton!!!!

非常感谢!!!!

回答by Chandra Sekhar Walajapet

i am not sure what have you tried with javascript on the above solution, even in fiddle i couldnt see any javascript

我不确定您在上述解决方案中使用 javascript 尝试了什么,即使在小提琴中我也看不到任何 javascript

i would recommend you to go through the below posts

我建议您阅读以下帖子

Using the GET parameter of a URL in JavaScript

在 JavaScript 中使用 URL 的 GET 参数

the above post will help get the parameter "sku"

上面的帖子将有助于获取参数“sku”

now if you have any value in the same you can set the iframe src by

现在,如果您有任何相同的值,您可以通过以下方式设置 iframe src

document.getElementById("iframeid").src = "default.html?sku="+sku

回答by Robyflc

Just find the '?' in window location and set the src:

只需找到“?” 在窗口位置并设置src:

var url = window.location;
var pos = url.indexOf('?');
var query = url.substring(0,pos+1);

then set it with JQuery:

然后用 JQuery 设置它:

$('iframe').attr('src','/default.html?'+query);

回答by Man Programmer

<iframe id="changeIframe" src="http://hosted.where2getit.com/asics/index.html" frameborder="1" width="920" height="635" scrolling="no"></iframe> 

document.getElementByid("changeIframe").src='http://hosted.where2getit.com/asics//default.html?sku=T235N&addressline=90803&form=product_search"'

回答by muthu

You may try like this

你可以试试这样

$(function () {
    var url = parseUrl(window.location.pathname).search;
    if (url.match("sku").length > 0) {
        $('#iframe1').attr('src', '/default.html' + url);
    }
});

function parseUrl(url) {
    var a = document.createElement('a');
    a.href = url;
    return a;
}

And the jsfiddle

jsfiddle

回答by Jared Farrish

First, give your iframean idso we can getElementById:

首先,给你iframe一个,id这样我们就可以getElementById

<iframe src="http://hosted.where2getit.com/asics/index.html" id="sku_frame"

Then, setup a script to run on page load, detect if there's an sku=key in the location.searchstring, and grab the iframereference and modify it's el.src:

然后,设置一个脚本以在页面加载时运行,检测字符串中是否有sku=location.search,然后获取iframe引用并修改它el.src

(function load(){

    if (window.addEventListener) {
        window.addEventListener('load', run);
    } else if (window.attachEvent) {
        window.attachEvent('onload', run);
    }

    function run() {
        var hassku = !!(location.search.indexOf('sku=') + 1);

        if (hassku) {
            document.getElementById('sku_frame').src = '/default.html' + location.search;
        }
    }

})();

http://jsfiddle.net/userdude/rUnQd/6/

http://jsfiddle.net/userdude/rUnQd/6/