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?

NewCodeMan
  • 227
  • 4
  • 16

2 Answers2

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.

The KNVB
  • 3,588
  • 3
  • 29
  • 54
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?

Beomi
  • 1,697
  • 16
  • 18