简体   繁体   中英

How can I pull a segment of the current page URL (last five digits) into an iFrame within the page, in Wordpress?

Our website uses Wordpress, and we pull our job openings from the software using an iFrame. The iFrame query requires us to get the job "code" from the URL (eg xyz.com/job-details/jobcode=11568 ).

The vendor has provided javascript that is supposed to do this, but it does not work. When the page loads the src just outputs as src(unknown)". The iframe should read as src="https://evoportalus.tracker-rms.com/COMPANY/jobs?fields=title,location&filters=Reference|<<the jobcode pulled from the URL>>

Can anyone help?

<script type="text/javascript">
loadSite();
</script>

<iframe src="" frameborder="0" scrolling="yes" width="700" height="700" id="trmsjobs"></iframe>

<script type="text/javascript">
    var getQueryString = function (field, url) {
        var href = url ? url : window.location.href;
        var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
        var string = reg.exec(href);
        return string ? string[1] : null;
    };
    function loadSite() {
        var site = "https://evoportalus.tracker-rms.com/LincolnStrategic/jobs";
        site += "?fields=title,location&filters=reference|" + getQueryString("jobcode");
        document.getElementById('trmsjobs').src = site;
    }
</script>

On Edit

Apologies, I don't think my original explanation was clear enough. The getQueryString variable seems to work; it outputs just fine. The issue is that the "site" variable is not being inserted into the SRC value in the iFrame, which is what this script is supposed to do.

Tweak your code to move the function call into the same block and change var functionName = function syntax to function functionName syntax


<iframe src="" frameborder="0" scrolling="yes" width="700" height="700" id="trmsjobs"></iframe>

<script type="text/javascript">
        loadSite();
    function getQueryString (field, url) {
        var href = url ? url : window.location.href;
        var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
        var string = reg.exec(href);
        return string ? string[1] : null;
    };
    function loadSite() {
        var site = "https://evoportalus.tracker-rms.com/LincolnStrategic/jobs";
        site += "?fields=title,location&filters=reference|" + getQueryString("jobcode");
        console.log(site);
        document.getElementById('trmsjobs').src = site;
    }
</script>

You can split the string with "/", then look for each component for the parameter you want, splitting this time with "=". Not a clean solution, but works:

var getQueryString = function(field, url){
    var ret=null;
    url.split("/").forEach(function(v){
        var s=v.split("=");
        if(s.length>1 && s[0]==field){
            ret=s[1];
            return;
        }
    });
    return ret;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM