最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Set iFrame src from window.location - Stack Overflow

programmeradmin0浏览0评论

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

How can I set an iframe src like this:

  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"

/

Thank you a ton!!!!

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

How can I set an iframe src like this:

  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"

http://jsfiddle/Digitalctn/rUnQd/

Thank you a ton!!!!

Share Improve this question asked Sep 27, 2012 at 6:07 user1410949user1410949 111 gold badge1 silver badge3 bronze badges 3
  • Use console.log(window.location) in Firebug or Chrome Console and take a look at what's in that object. Then use window.addEventListener/window.attachEvent to listen to page load and set the iframe's src property with what you find useful in the location object. – Jared Farrish Commented Sep 27, 2012 at 6:13
  • Jared thank you but I'm a jQuery rookie. How do I code that? – user1410949 Commented Sep 27, 2012 at 6:16
  • Take a look at this fiddle: jsfiddle/userdude/rUnQd/6 Note, it won't do anything since there's no sku to be found. But that should work in practice. – Jared Farrish Commented Sep 27, 2012 at 6:26
Add a ment  | 

5 Answers 5

Reset to default 1

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

i would remend you to go through the below posts

Using the GET parameter of a URL in JavaScript

the above post will help get the parameter "sku"

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

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

First, give your iframe an id so we can getElementById:

<iframe src="http://hosted.where2getit./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.search string, and grab the iframe reference and modify it's 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/userdude/rUnQd/6/

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

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

then set it with JQuery:

$('iframe').attr('src','/default.html?'+query);
<iframe id="changeIframe" src="http://hosted.where2getit./asics/index.html" frameborder="1" width="920" height="635" scrolling="no"></iframe> 

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

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

发布评论

评论列表(0)

  1. 暂无评论