4

The situation is as follows - there is a page A.aspx which has a hyperlink to another page B.aspx I want to run some javascript after someone clicks on the hyperlink in A.aspx and after B.aspx loads BUT I do not want to manually add this javascript to B.aspx?

The background for this is as follows- In a sharepoint site collection all the pages use a masterpage but there can be some pages which are standalone and these don't use a masterpage. The task is to add javascript to every page in the site (standalone as well as regular). So I added this javascript to the masterpage but since these standalone pages don't use the masterpage, they are not loading the javascript. The situation is that some regular sharepoint pages have hyperlinks pointing to these standalone pages, so when someone clicks on the hyperlink on the regular sharepoint page the standalone page gets loaded. Since there are quite a lot of standalone pages and I don't want to touch them, I am thinking if there is any way to execute javascript on these pages after they load (as a result of a user clicking the hyperlink on the regular sharepoint page) ?

2
  • 3
    B.aspx has to load that script by itself. However if both pages have the same domain and B.aspx is opened via window.open then you can indeed execute scripts in B.aspx from A.aspx. Commented Sep 7, 2016 at 17:11
  • @Derek do you have any pointers about go about doing this? Commented Sep 7, 2016 at 17:49

1 Answer 1

1

I want to run some javascript after someone clicks on the hyperlink in A.aspx and after B.aspx loads BUT I do not want to manually add this javascript to B.aspx

You can't do that, no. (Not without requiring everyone to have a browser extension installed.) I'm afraid unpalatable as it is, if the standalone pages don't already have a script they load that you could add this new code to, you'll have to modify each of the standalone pages to include the new script.

I checked just now - the hyperlinks are like this <a href="javascript:void(0);" onclick="OpenPopUpPage('B.aspx?ItemID=65','',500,350);">test link</a> they seem to be using OpenPopUpPage

That's a completely different thing. If the OpenPopUpPage is using window.open, and the A.aspx page sticks around while B.aspx opens, it's possible for code in A.aspx to do things with the B.aspx page when it opens (assuming they're on the same origin, which presumably they are in your case).

You'll need to find the source of OpenPopUpPage and find its call to window.open, hook an event on the resulting window (DOMContentLoaded or, worst case, load), and then make your modifications to the page in the event handler:

var url = "B.aspx";
var wnd = window.open(url);
checkChildReady();
function checkChildReady() {
    if (wnd.document && String(wnd.document.location).indexOf(url) != -1) {
        if (wnd.document.readyState != "loading") {
            windowReady();
        } else {
            wnd.document.addEventListener("DOMContentLoaded", childReady, false);
        }
    }
}
function childReady() {
    // Do your stuff here
}
Sign up to request clarification or add additional context in comments.

6 Comments

@t-j-crowder what you think about the comment made above by Derek ? the pages (regular and standalone) share the same domain and I am guessing that the standalone pages open via window.open
@vivekm: What do you mean you're "guessing" that they're opened with window.open? Can't you look? But in the question, you said it was a link from one page to the next and you wanted to execute code on the target after the user followed the link. That's a completely different scenario to window.open. It's that sceanrio the answer applies to.
@t-j-crowder I said I am guessing because I am not really sure whether window.open is used when a hyperlink is clicked or the standalone page opens by some other mechanism. I will have to check.
@t-j-crowder I checked just now - the hyperlinks are like this <a href="javascript:void(0);" onclick="OpenPopUpPage('B.aspx?ItemID=65','',500,350);">test link</a> they seem to be using OpenPopUpPage
@vivekm: That's completely different from normal link processing. I've added a note about it to my answer.
|

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.