How to run a script when clicking on a link? - javascript

So, I have a javascript for a popunder ad, (yeah I know they're annoying but it's not an ad, it's a page that opens behind the main page) anyway, because the script has the page open automatically when you go to my page, pop up blockers automatically block it, so what I would like to do is have the script run when someone clicks a link on my page, therefore it is user initiated and not automatically, so it doesn't get blocked by a popup blocker.. basically, so when someone clicks on one of the links on my page, it opens the link, but also opens the pop under behind my page.. I'm kind of new to javascript, but any help is appreciated!

This would be one approach:
HTML:
<a id="link">Link</a>
JavaScript:
function script() {
alert("I'm the ad");
};
document.getElementById('link').onclick = function () {
script();
};
For demonstration see this Fiddle.
/Edit: Sure, here is the JavaScript:
// copy and paste the script from the website
document.getElementById('open').onclick = function () {
load_pop_power();
};
If you don't want to show the ad when the user visits the site, you could however delete half of the code.

You could do something like this
function doSomething() {
//do your actions here
window.location="http://www.gotothelink.com";
}
</script>
click me

When the user clicks the link, the javascript code in the "onlick" attribute is executed. "window.open" opens a new window and "return true" has the effekt that the normal behaviour of the link keeps working.
tiscover

So you need to bind the popunder to a click event on a link
see http://www.pagecolumn.com/javascript/bind_event_in_js_object.htm

Related

Confirm close window popup does not appear on first time

I am trying following code to confirm user if they really want to close window.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
But it does not work when my page load and I try to close page [X] without clicking anywhere on page. Yes it does appear when I click somewhere on page then close [X] window. I don't know why do I need to click somewhere on page and then close window.
What I tried so far but nothing works.
window.focus() on document ready as it gets focus on window.
$('#someElementOnpageId').click() as it gets auto click on document
ready.
Can anyone please suggest what should I do?

Open link in same page and once link is open, execute some code

I have several pop-ups on home page. I open and close them by selecting them with ID and using fadeIn() and fadeOut(). Now I want to open a specific pop-up by clicking on link from another window? For example, if from that new window I click on 'Pop Up 1', I want home page to open and then show 'Pop Up 1'.
I tried using this code below but while writing this code I realized that the script gets reloaded and thus my function of loading a pop-up does not work.
So my question is, is there some elegant solution you could recommend to show element in one page while a link that specifies which element has to be shown is in another?
$("#galleryNav a").on('click', function() {
window.open("/pixeleyes",'_self',false);
setTimeout(function() {
var popToShow = $(this).attr('data-pop');
$(".text-content-outer").hide();
$("#" + popToShow).fadeIn();
}, 5000);
});
One idea might work is
When you are opening a new page using the below line then send some parameter or hash value with it.
window.open("/pixeleyes",'_self',false);
like
window.open("/pixeleyes#openpopup",'_self',false);
Then in the page ready of this page check if the hash exists open the popup otherwise do nothing.
Not sure if this is what you are looking for.
$("#galleryNav a").on('click', function() {
window.open("/pixeleyes#showpopup",'_self',false);
});
showpopup could be anything that you want to open as popup...

GWT - Show a custom dialog before window close?

I need to prompt the user to a site usability survey when the user navigate away from the site or try to close the site window. For example, when the user closes the window of my site, I want to show a prompt to say "do you want to take a survey?" , if the user clicks yet, open a new window with the survey link.
Is there a way in GWT to achieve that?
I have tried using
Windows.addWindowCLosingHandler
but that only gave me the ability to set a message to ask if the user want to stay on the site or now using ClosingEvent event , event.setMessage("sure?");
thanks
You can use window.onunload but that will be called whenever the user changes the page
Also see here: How to catch user leaving a page and cancelling it
Use the unload method on the body:
<body onUnload="unloadFunction(); return false;">
Then something like this for your js:
function unloadFunction()
{
if (confirm("Would you like to take our servey?"))
{
window.location.href = 'location_of_your_survey'
}
return false;
}

How to disable back,forward and Refresh functionality in browser?

Can anyone help me to disable browser back,forward, back button and right click menu functionality using JavaScript or JQuery? I have tried disabling the back button like this:
function disableBackButton() {
window.history.forward();
}
setTimeout("disableBackButton()", 0);
I have called this function in the Body onload event. I have a doubt that where we need to put this code in the Calling page or Called Page.Suppose i have two pages like this FirstPage.aspx and SecondPage.aspx. When I navigate from the First Page to Second Page when I click back button of the browser it should not go to FirstPage.aspx.
Have a look at A Thorough Examination of "Disabling the Back Button."

Anyone can close the popup generated by Jquery plugin bPopup, from a child iframe?

Okay, the story is abit long but hope you can bear with me so that there could be some background to the problem:
I'm developing the registration section for a portal using ASP.NET and to make things nice,
I decided to use bPopup from here to load the registration page into the iframe of the calling page (parent page). Now I need a way to close down the iframe after a successful registration so from the codebehind of the registration page (nested in the iframe) I tried a Javascript function from the parent that allows me to do so, here's the function to close the iframe generated by the plugin:
function bPopup_close() {
$(".bClose").closePopup();
return false;
}
and here's the code behind from the iframe (the function will be called on submit):
ClientScript.RegisterOnSubmitStatement
(GetType(), "Javascript", "javascript: window.opener.bPopup_close();");
I wasn't able to close the popup. Interestingly, Firebug showed me that after I clicked the submit button, window.opener was null. Does that mean that the parent was indeed closed? Anyway the popup was still there...
Hope you guys could share some insights on anything similar?
I can see that dinbror already answered you on his page :)
#fred: Glad you like it. Are you using
the newest version of bPopup?
Solution: Create a function on the
page which opens the popup:
function closeBPopup() {
$(selector).bPopup().close() }
Then you can trigger it inside your
iframe whenever you are done doing
your stuff with:
parent.closeBPopup();
After half a day, I finally got it working with:
ClientScript.RegisterStartupScript
(GetType(), "blah", "< script type=\"text/javascript\">bclose(); < /script> ", false);
and the code bclose() is:
function bclose() {
parent.$("#popup").bPopup().close();
return false;
}

Categories