Redirect From a Js File - php

I am using this code in php or html file for redirect
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script language="JavaScript">
var country= geoip_country_code();
if(country == "US" )
{
<!--
window.location = "http://google.com"
//-->
}
</script>
but i want to use a js file which do the same function but its extension should be .js
as http://domain.com/redirect.js so what would be the code?

This includes a script include function with a callback, so that the Maxmind client code won't run until the Maxmind library has been loaded.
loadScript("http://j.maxmind.com/app/geoip.js", function() {
var country = geoip_country_code();
if (country === "US") {
window.location = "http://google.com/";
}
});
function loadScript(url, callback) {
// adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
// then bind the event to the callback function
// there are several events for cross browser compatibility
script.onreadystatechange = callback;
script.onload = callback;
// fire the loading
head.appendChild(script);
}

Related

like.js:2 Uncaught ReferenceError: $ is not defined

I want to make facebook like button.when i click like.undefined $ is appearing in console.it should echo 22 value default value on click.
function like_add(article_id) {
$.post('ajax/like_add.php', {article_id:article_id}, function(data){
if(data == 'success')
{
like_get(article_id);
}
else
{
alert(data);
}
});
}
function like_get(article_id) {
$.post('ajax/like_get.php',{article_id:article_id}, function(data){
$('#article_'+article_id+'_likes').text(data);
});
}
foreach($articles as $article){
echo '<li>',$article['article_title'],'</p><p><a class="like" href="#"
onclick="like_add(', $article['article_id'] ,');">Like</a>
<span id="article_', $article['article_id'] ,'_like">', $art
jQuery is not included in current page.
You can include it with this code:
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
Place it between <body> </body> before your code.
But also you can load it async way:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://code.jquery.com/jquery-2.2.1.min.js";
script.onreadystatechange = handler;
script.onload = handler;
head.appendChild(script);
function handler() { console.log("jQuery loaded!"); }
handler() will be called when jQuery will be loaded.

Loading Javascript with AJAX

I have this neat little code:
<script type="text/javascript" language="javascript">
$(function(){
$(window).hashchange( function(){
window.scrollTo(0,0);
var hash = location.hash;
if (hash == "") {
hash="#main";
}
var newhash = !/\.php$/i.test(hash)? hash+".html": hash;
ajax_loadContent('m',newhash.slice(1,newhash.length));
$('.menu li a').each(function(){
var that = $(this);
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'current' );
});
})
$(window).hashchange();
});
</script>
The only problem is that if I navigate to pages containing Javascript that code isn't being executed. How can I make it run the Javascript code as if the browser loaded the page normally?
It will not be execute as it comes as text. You have to use "eval" function on your response text

Exit Popup With Respone Redirect

I have a requirement to show an Exit Popup on one of our processing pages. I am showing the exit popup by registering a client side javascript like
window.onbeforeunload = ShowExitPopUp();
The popup executes fine if you close the window, or try to type in a different URL. However the issue is that my processing page is also performing a "Response.Redirect" on the server side once the process completes. When response.redirect happens the exitpopup also shows up. Is there a way to have the exit popup not show up in that case?
this is the code below which i am using
<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>
<script type="text/javascript">
var ShowExitPopup = true;
function ExitPage()
{
if (ShowExitPopup)
{
ShowExitPopup = false;
location.href = "http://www.YOURURL.com/destination";
return '****************************************\n\YOUR HEADLINE\n\n YOUR TEXT1 \n\n YOUR TEXT "\n\n | |\n | |\n V V\n 96+ v\n ********************************************************\n';
}
}
</script>
if (document.getElementById('<%=Panel.ClientID %>').style.display == "") {
$find("<%= Popup.ClientID %>").hide();
document.getElementById("<%= btncloseAcc.ClientID %>").click();
}
try this..........

Exit Popup Redirect but only execute on exit not on clicking any other html redirect button on page

l am using this code
var exitPop = false;
var nonFire = false;
window.onbeforeunload = function () {
if(!exitPop){
exitPop=true;
return 'Wait! YOU ARE TODAYS WINNER!';
}
};
setInterval(function(){
if(exitPop && !nonFire){
nonFire = true;
window.location.href = 'http://google.com';
}
}, 200);
but its also execute on clicking any html redirect button on page.. i want it execute only if someone close browser and it should support all browsers.
i need to add this at only one link in my site how can id do? i mean i am using this code for redirect
<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>
so i want exit function do not execute for this.. so how do integrate this
I haven't tested it but you should be able to attach a 'click' event to every link on the page, which should set a global variable such as
linkClicked = true;
You can then check that variable in the unload event
if (!linkClicked) // variable is false so they must not have clicked on a link
{
// Some annoying message here
}
Disclaimer: this is pretty much pseudo code, it's not a copy+paste solution.
window.onbeforeunload does not distinguish between links, back/forward buttons, exit buttons or anything else. If fires when you leave the page, regardless of how you leave the page.

Can xmlhttp.responseText contain <script type="text/javascript src="..."></script> and load?

The script does window.open('',...) and then writes xmlhttp.responseText by doing innerHTML=xmlhttp.responseText, but the script doesn't load.
Typically, you would get the xmlhttp request response as pure Javascript and then use the Javascript eval() function:
function callback() {
var result = xmlHttp.responseText;
eval(result);
}
In this case, you would NOT treat it as HTML. Instead, you would return pure code. The difference:
Don't do this in your page you call
<script type="text/javascript">
alert('xmlhttprequest loaded!');
</script>
Do this instead
alert('xmlhttprequest loaded!');
So that, in effect this is what happens:
function callback() {
var result = xmlHttp.responseText;
eval("alert('xmlhttprequest loaded!');");
}
But you don't want this to happen:
function callback() {
var result = xmlHttp.responseText;
eval("<script>alert('xmlhttprequest loaded!');</script>");
}
There are some issues associated with doing it this way, such as eval can be slow. Google javascript eval to see what others have to say.
=== EDIT ===
Using a DOM method as opposed to xmlhttprequest may actually be what the original poster is needing to do here, which is load a Google captcha code dynamically.
<html>
<head></head>
<body>
<script type="text/javascript">
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "http://google.com/captcha/location";
head.appendChild(script);
</script>
</body>
</html>
Additionally, if you need it to land somewhere, you can do something like:
<html>
<head></head>
<body>
<div id="captcha">
</div>
<script type="text/javascript">
var captcha = document.getElementById('captcha');
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "http://google.com/captcha/location";
captcha.appendChild(script);
</script>
</body>
</html>
Take a look at this write-up
eval(xmlhttp.responseText);
You might want to eval the responseText in the javascript.
You might want to make sure that the responseText comes from your own server-side system, to avoid XSS attacks.

Categories