I also dont like exit popups but i want to integrate if for a comming festival day so want to thanks my visitors and gave them some thing special.
I am this code below for redirect on exit.
var exitPop = false;
var nonFire = false;
window.onbeforeunload = function () {
if(!exitPop){
exitPop=true;
return 'Happy Festival Day!';
}
};
setInterval(function(){
if(exitPop && !nonFire){
nonFire = true;
window.location.href = 'http://google.com';
}
}, 200);
I am using this code below its basically redirect on doing a comment
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.Event.subscribe('comment.create',
function (response) {
window.location = "http://domain.com";
});
FB.Event.subscribe('comments.remove',
function (response) {
window.location = "http://domain.com";
});
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
//]]>
</script>
and i dont want to show exit redirect to the my visitors who comment because its annoy my visitors i want that code execute only on clicking exit. so what code i should include which create exception of comment redirect and should not show popup to commenter
A popup window will only not be blocked if some link opens it. So if you are trying to load something on exit it won't work.
This is a working example: http://swip.codylindley.com/popupWindowDemo.html using a little library in jquery someone wrote. You can check out the source.
Basically, if the popup window opens onclick you should be fine, but in any other cases it will be blocked.
I am not sure how certain companies get away with it, but any sort of hacks eventually get plugged. One way I attempted to do it was to trigger an onclick action on a link and even that didn't work. So it is fairly fool proof.
You could do this by setting a cookie in javascript. You could do this with straight javascript but the code is shorter with the jquery.cookies plugin so ill demonstrate that method.
In your comment.create function, set a cookie to show that this user has made a comment:
FB.Event.subscribe('comment.create',
function (response) {
$.cookie('has_commented', 'true');
window.location = "http://domain.com";
});
Then check the cookie value on your exit function:
if($.cookie('has_commented') == true) {
return 'Happy Festival Day!';
}
Jquery Cookies plugin here: https://github.com/carhartl/jquery-cookie/blob/master/README.md
or if you want to do the same thing with straight javascript - same concept - but a bit messier.. info here: http://www.w3schools.com/js/js_cookies.asp
Related
Simple JavaScript redirect window.location.href = https://example.org/Page.aspx?q=123 to .aspx resource causes page to navigate to https://example.org/undefined?q=123.
Manual navigation to the address works as expected.
I am not familiar with ASP.NET much so I was wondering if that's a common problem or is it only specific to the project that I am working on?
If we assume that some script modifies the URL later down the line, is there any way to stop that?
e.preventDefault()? Or stopImmediatePropagation()?
Thanks!
UPDATE:
Used code snippet:
$('body').on('keydown', selectorSearchBoxInput, function (e) {
var inputValue = $(this).val();
if (e.keyCode == 13 && !!inputValue) {
var newHref = 'https://example.org/Category/SubCategory/Page.aspx?q=' + encodeURIComponent(inputValue.trim());
window.location.href = newHref;
}
});
On clicking a hyperlink,
{{id}}
I require something to open up in a new tab based upon the response (a flag, lets say - responseFlag) that I get from the ajax call.
Two methods I tried.
Note : Function is dependent on the id, so not using it on page load.
1.
$http.get(url).success(function(response) {
if(response.data.responseFlag==true){
$window.open("http://www.example.com");
}
else{
//perform something else other that window.open
}
});
The problem here is the 'Popup blockers' in the browsers (Chrome, Mozilla) - that keeps blocking them.
2.
var w = $window.open("","_blank");
$http.get(url).success(function(response) {
if(response.data.responseFlag == true){
w.location = "http://www.example.com";
}
else{
//perform something else
}
});
Here, if the 'responseFlag = true', it opens up in new tab. But as you might have guessed it, for 'responseFlag = false' too the tab opens up. I can use w.close() in the 'else' perhaps. But I think that's not a solution.
Help me out friends !
In your second example you're openning the new tab before doing the request, so it doesn't matter wich callback gets executed, you'll get a new window openned.
I don't see any reason why $window.open(...) works with your second example but not in the first, maybe is because the "_blank" parameter...
So, can you try this way?
$http.get(url).success(function(response) {
if(response.data.responseFlag == true){
var w = $window.open("","_blank");
w.location = "http://www.example.com";
}
else{
//perform something else
}
});
I'm trying to build an off-site notification function in jQuery. The script first checks if the link is an external link and then checks against a db table entries for exceptions. If the link is external and not on the list of exceptions, send the visitor to a notification page. If it is an external link that's on the exception list, then open the link in a new window without the notification page.
I'm using a jQuery $.post call to send the link info out to a php script that retrieves the exceptions and returns a yes or no for if it needs to go to the notification screen. Here's the code:
$('a').click(function(){
var url =$(this).attr('href');
if(url !== '#'){
// ignore links that don't 'go' anywhere
if($(this).hasClass('alerted')){
// .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
window.open(url);
return false;
}else if(url.substr(0,4) !='http'){
// check that the url isn't an internal link ('/page.php' for example)
return true;
}
// ajax script to check url is external and is there an exception. Returns as json object:
// link: link
// notify: true/false
$.post("/scripts/form_process.php", { action : 'offsite', link: url}, function(data){
if(data.notify == true){
// if visitors should be notified, redirect to the following link:
window.location= '/leaving-site?link='+encodeURIComponent(data.link);
return false;
}else{
// if the link is in the exception list, don't notify but do open the link in a new window:
window.open(data.link);
}
});
return false;
}
});
This is working fine except that so long as the window.open(url) command is inside the $.post success function, the browser is treating it like a popup instead of as a natural link. This seems to be a problem when using window.open inside the ajax call as far as I can tell. When I use it here:
if($(this).hasClass('alerted')){
// .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
window.open(url);
return false;
}
I don't get the pop up blocker.
I can't hard code the exceptions list and I have to check every link - I can't assume a class will be added to the links that need to be notified for example.
How can I open the external link in a new tab and avoid the popup blocker in this code?
The classic way to solve this is as follows:
Create the new window before the AJAX call:
var newWindow = window.open('', '_blank');
And in the success - you assign the URL to the new window like so:
newWindow.location.href = 'http://example.com';
Full example with your code:
$('a').click(function(){
var url =$(this).attr('href');
if(url !== '#'){
// ignore links that don't 'go' anywhere
if($(this).hasClass('alerted')){
// .alerted is used on the notification page to show the user's already been notified & prevents an infinite loop of notifications.
window.location = url;
return false;
}else if(url.substr(0,4) !='http'){
// check that the url isn't an internal link ('/page.php' for example)
return true;
}
// ajax script to check url is external and is there an exception. Returns as json object:
// link: link
// notify: true/false
var newWindow = window.open('', '_blank');
$.post("/scripts/form_process.php", { action : 'offsite', link: url}, function(data){
if(data.notify == true){
// if visitors should be notified, redirect to the following link:
newWindow.location.href= '/leaving-site?link='+encodeURIComponent(data.link);
return false;
}else{
// if the link is in the exception list, don't notify but do open the link in a new window:
newWindow.location.href(data.link);
}
});
return false;
}
});
I need to move to another page after some data is returned from a $.post() ?
verifystudent.html:
$.post('students/verify.php',
function(results){
preventdefault();
location.replace("verifystudent.html");
window.location.href = "completed/student_result.html;
}
Basically when the data is received to the verifystudent.html it should move to student_result.html and when the browser back button is clicked it should avoid going to verifystudent.html. How can I do this?
Try this script it prevents browser's back button so use it in verifystudent.html
<script type = "text/javascript" >
history.pushState(null, null, 'pagename');
window.addEventListener('popstate', function(event) {
history.pushState(null, null, 'pagename');
});
</script>
Hope this helps.
Use SessionStorage to save the state of your application. You can create a key to mark that a student is verified.
Check for this key when the verification page loads and use location.replace to change the URL if the student is already verified.
Also read this article about preventing users from going to the previous page.
For your second question "and when the browser back is clicked it should avoid going to verifystudent.html"
I was searching for the same question and I found following code on a site. Thought to share it here:
function noBack()
{
window.history.forward()
}
noBack();
window.onload = noBack;
window.onpageshow = function(evt){ if(evt.persisted) noBack(); }
window.onunload = function(){ void(0); }
However as noted by number of developers, this is never a good practice and should be avoided for all reasons.
You could use the JQuery navigation event on verifystudent.html to send the user to the desired page
$(window).on("navigate", function (event, data) {
var direction = data.state.direction;
if (direction == 'back') {
window.location.href = "completed/student_result.html;
}
}
Somebody entered XSS code in my friend's site. It inserts <script>alert(0)</script> in the page source. You can see it here.
Is there a way to remove this from the page at runtime, to prevent it from being executed?
He has a presentation on it tomorrow and he has no access to database to remove it.
As a really quick fix. If he has access to the javascript he could do a simple trick like below.
alert = function() {}
This will stop all alerts from firing.
You can disable alert on http://technoflexusdatastreamapi.appspot.com/landingpage before loading the data:
$('.viewDataLink').click(function ()
{
$("#scriptShow").hide();
$("#showData").show();
$("#progressAnimation").show();
var tmpAlert = alert;
window.alert = function () { };
$("#showData").load("/listData", function (response, status, xhr)
{
if (status == "success")
{
$("#progressAnimation").hide();
window.alert = tmpAlert;
}
});
});