0

I am writing a JavaScript function that when the user clicks a link from one page, it goes to this page. When the page loads, it should open up a popup box. The problem is I don't know how to tell it in the function to open up onload if the user is coming from a certain link. Otherwise the page should act normal without a popup onload.

 window.onload = function() {
 var opened = window.open('', 'height = 500, width = 500');
 opened.document.write('Open');
 }
2
  • 1
    "it should open a popup box"... Not sure that's something that necessarily SHOULD happen anywhere :) Anyway, +1 to adding a query string parameter in the link to the other page. Commented Apr 18, 2014 at 18:38
  • Lol +1 for knocking on popups! Commented Apr 18, 2014 at 18:40

4 Answers 4

1

You can add a hash fragment, then check for that hash fragment in the url. If it is there call the function. :)

HTML

<a href="your_page.html#dothething">Click me</a>

JS

window.onload = function() {
  if (window.location.hash === "#dothething") {
    //Call your functions
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah thats what I'm trying to do, using a fragment but I don't know how to write that.
Yeah, you could do window.location.search.indexOf("dothething") > -1 with ...index.html?dothething...
1

When you redirect your user you should add a QueryString parameter, in the onload of your 2nd page you can then test on this QueryString.

var url = "http://google.com?parameter1=value1&parameter2=value2";
window.open(url, 'height = 500, width = 500');

1 Comment

how would it look if I wanted to add a QueryString?
1

Building on what Hatsjoem answered first you would put the variable in the request coming from the link. Like so : http://www.Blah.com/NeatoPage.aspx?YourVariable=X.

Then in the Page_Load code behind of the popup page check the value of the querystring variable.

Request.QueryString["YourVariable"].toString() or Request.QueryString["YourVariable"] != null

Put the result in a hiddenfield or something.

Then just have your javascript check the hiddenfield for decision making.

document.getElementById("hdnVariable").value

Make sense?

Comments

1

You can use document.referrer http://www.w3schools.com/jsref/prop_doc_referrer.asp.

window.onload = function() {
    if(document.referrer.indexOf("keySubstringOfReferrerUrl")!== -1){
         /*Do something*/
    }else{
         /*Do the other thing*/  
    }
}

This works with normal links (anchor tags with href). If you did the links with javascript you may need to use this function in order to work with older versions of IE:

function navigateWithReferrer(url) {
    var fakeLink = document.createElement("a");
    if (document.createEvent) {
        location.href = url
    } else {
        fakeLink.href = url;
        document.body.appendChild(fakeLink);
        fakeLink.click()
    }
}

Comments

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.