How do I pass URL parameter to an iFrame in Wordpr

2019-08-01 16:44发布

Wordpress 3.2.1. I have a page which has an iframe embed of an external page. i.e. in WP page I have the following code

<iframe src="http://external.website/page" width="100%" height="300">

Is it possible to that I can dynamically set the external website source? via URL parameters? Suppose the above page was http://mysite.com/myiframe-page. If the page URL was http://mysite.com/myiframe-page?country=sg&currency=sgd, I would like my iFrame SRC get the URL params. So iframe URL for the above page will be

<iframe src="http://external.website/page?country=sg&currency=sgd" width="100%" height="300"></iframe>

Is this possible in wordpress? how? I am very new sorry.

标签: wordpress
1条回答
Fickle 薄情
2楼-- · 2019-08-01 16:49

Yes, iFrame can do this. However it is an old way of coding. I would recommend a Ajax request or some PHP include rather than iFrames.. sometimes in this case, it seems a quicker hack.

Simply change your code from this:

<iframe src="http://external.website/page?country=sg&currency=sgd" width="100%" height="300"></iframe>

to

<iframe src="http://external.website/page?country=sg&currency=sgd" width="100%" height="300" name="iframeTarget" id="iframeTarget"></iframe>

And now with jQuery or some JavaScript, you can change the iframe URL dynamically as you have a named target and and ID.

Example.

<a href="yoursite.php?andStrings=uptoyou&page=1" onclick="return loadIframe('iframeTarget', this.href)"> change link 1 </a>

The jQuery

    function loadIframe(iframeName, url) {
    var $iframe = $('#' + iframeName);
    if ( $iframe.length ) {
        $iframe.attr('src',url);   
        return false;
    }
    return true;
}
查看更多
登录 后发表回答