0

I have two sites. Site "A" and Site "B"

"A" has a JS function that opens a new page and loads site "B"

function siteB_link() {
     window.open("https://www.siteB.com");
}

Site "B" is a single paged site index.html built with <div> tags - It has <div> tags that I need to control through JS functions that work fine once on site "B"

Like I mentioned its a single page site, so there are no different internal URLs to jump to.

Something like: www.siteB.com/funcName() do something

I know that wont work at all, but something that can do that.

Is this possible?

2 Answers 2

1

Sure you can do with GET QueryString.

Like this:

www.siteB.com/?jsFuncName=1

You can access with this code below:

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var foo = getParameterByName('jsFuncName'); // '1'

if you execute this code when event DOMContentLoaded triggered(in jQuery, 'onload'), then you can just do like this:

<script>
    document.addEventListener('DOMContentLoaded', function () {
        function getParameterByName(name, url) {
            if (!url) url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }
        var foo = getParameterByName('jsFuncName'); // '1'
        if (foo === '1') {
            // execute function you want
        }
    })
</script>

REF: How can I get query string values in JavaScript?

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate the detailed answer. and your time to write all that! I will absolutely try this.
1

You cannot use www.siteB.com/funcName().

But you can use www.siteB.com/?function='abc'.

In page B, you can use window.location.href to get the url, and then "extract" the "function" value.

Finally, you can base on the "function" value to varies action.

1 Comment

Thank you, I will try this as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.