I'm trying to make a redirect page that tracks the loading of that page with mixpanel.track
The problem is, the page redirects before the async javascript has time to download and run. What do I do?
Here is my code.
Edit: I'm putting the code in the question.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<!-- start Mixpanel --><script type="text/javascript">(function(f,b){if(!b.__SV){var a,e,i,g;window.mixpanel=b;b._i=[];b.init=function(a,e,d){function f(b,h){var a=h.split(".");2==a.length&&(b=b[a[0]],h=a[1]);b[h]=function(){b.push([h].concat(Array.prototype.slice.call(arguments,0)))}}var c=b;"undefined"!==typeof d?c=b[d]=[]:d="mixpanel";c.people=c.people||[];c.toString=function(b){var a="mixpanel";"mixpanel"!==d&&(a+="."+d);b||(a+=" (stub)");return a};c.people.toString=function(){return c.toString(1)+".people (stub)"};i="disable track track_pageview track_links track_forms register register_once alias unregister identify name_tag set_config people.set people.set_once people.increment people.append people.track_charge people.clear_charges people.delete_user".split(" ");
for(g=0;g<i.length;g++)f(c,i[g]);b._i.push([a,e,d])};b.__SV=1.2;a=f.createElement("script");a.type="text/javascript";a.async=!0;a.src="//cdn.mxpnl.com/libs/mixpanel-2.2.min.js";e=f.getElementsByTagName("script")[0];e.parentNode.insertBefore(a,e)}})(document,window.mixpanel||[]);
mixpanel.init("9f85e6edf009562d5ac2c944b0da6398");mixpanel.track("CBhopID_x3cw17x1h")</script><!-- end Mixpanel -->
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta content="1;url=http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/" http-equiv="refresh" />
<script type="text/javascript">
window.location.href = "http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/"
</script>
<title>Redirecting...</title>
</head>
<body>
<p>If you are not redirected automatically, follow this link.</p>
</body>
</html>
mixpanel.track() has callback option which will be called after tracking event, you need to use it, on callback you could redirect to some location, place your window.location code inside the callback function, like:
mixpanel.track("CBhopID_x3cw17x1h", {}, function() {
window.location.href = "http://e20f07o0yd5b2re0-x3cw17x1h.hop.clickbank.net/";
});
Related
I'm having an issue where I'm trying to set a page's redirect destination to a URL from a JS function.
I've tried calling the function by using <meta http-equiv="refresh" in the header, but I either have the syntax wrong or <meta> simply doesn't allow for calling functions. I'm honestly not sure.
<head>
<script src="extFile.js"></script>
<meta http-equiv="refresh" content="1; go2();" id="levP" name="levP">
<title>SO Question</title>
</head>
go2() is a function from extFile.js which contains an if/then statement that provides different URLs depending on time of day. I'd like to have index.html redirect users via function go2() either by a method in the header or in the body.
If this should be handled in the body then I'd appreciate any feedback as to how that should look.
Like this? this code will redirect your page after 5 seconds
<head>
<script src="extFile.js"></script>
<script>
setTimeout(function (){
window.location = "https://stackoverflow.com/questions/57717282/how-to-set-redirect-to-links-provided-by-js-function";
}, 5000);
</script>
<title>SO Question</title>
</head>
if you want a to call a function do this:
<head>
<script src="extFile.js"></script>
<script>
var check = function(){
window.location = "https://stackoverflow.com/questions/57717282/how-to-set-redirect-to-links-provided-by-js-function";
}
check();
</script>
<title>SO Question</title>
</head>
I develop a single page web application, i want to block back button to prevent the music stops when an user click to it! can anyone help me please!
Ok, there is complete, commented and tested example:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
<script>
// Add actual page to history
history.pushState(null, null, location.pathname)
// Register back button click
window.onpopstate = function (e) {
// Prevent default action on back button click
e.preventDefault()
// Add actual page to history again
history.pushState(null, null, location.pathname)
}
</script>
</head>
<body>
</body>
</html>
I want to run function click on spesific id inside a frame, so i make two html file and i write the script like this
the main html is this index.html
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function autoClick(){
document.getElementById('framing').contentWindow.document.getElementById('trig').click();
}
</script>
</head>
<body onload="autoClick();">
<iframe name="framing" id="framing" src="testing.html" frameborder="0" scrolling="no" height="1200px" width="100%"></iframe>
</body>
</html>
and another one is the testing.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
window.onbeforeunload = function(){
return false; // This will stop the redirecting.
}
</script>
</head>
<body>
<div>
<a id="trig" href='http://www.redirect.com/' target='iName'>Home</a>
</div>
<iframe src="about:blank" name="iName" frameborder="0" scrolling="no" height="1200px" width="100%"></iframe>
</body>
</html>
so when index.html finish load the page, i want the function to click on the <a id=trig" href='http://www.redirect.com/' target='iName'>Home</a>
on testing.html that inside frame framing
and when it clicked, the trig will load a site http://www.redirect.com that will be contained inside iName frame, but the site http://www.redirect.com have a script that will redirect the TLD to the their site, so i put the script for preventing the page to break out the iframe `
window.onbeforeunload = function(){
return false; // This will stop the redirecting.
}
this two html file is hosted on same domain. but when i try to run it, the function click is not triggered, anyone can help me?
Thank you, and sorry for the bad english, it is not my native language
I want to be able to open a popup using window.open and subscribe to page events (onload etc) of the popup in the opener. So i'd want a method in my opener (parent page) to execute when the popup's onload or ready fires. Is this possible using plain js or jquery? Pls don't ask me why i want to do this - this can solve a lot of issues for me.
First page (x.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
var w = window.open('y.html', 'w');
w.document.getElementById('target').onclick = function () { alert('!'); };
</script>
</body>
</html>
Second page (y.html):
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="target">target</button>
</body>
</html>
Works for me...
*Update: Ultimately I've decided that accomplishing exactly what I want here isn't possible due to the issues it poses to security. Kalle's answer below gives a solution that is closest to what I want to accomplish.
In order to solve my problem I've created scripts on both pages and will use a sort of push notification that is routed through the server in order for them to communicate.
Thanks for the help!! *
I have two pages. Both windows already exist independently. Page two has a function declared in JS.
I would like to be able to call the function in window two by clicking a link in window one.
Page 1:
<html>
<head>
<title>This is a title!</title>
</head>
<body style="background: lightblue">
Click Me!
</body>
Page 2:
<html>
<head>
<META HTTP-EQUIV="Window-target" CONTENT="my_target" />
<title>This is a title!</title>
<script type=text/javascript>
function clicked() {
alert('test');
}
</script>
</head>
<body style="background: lightblue">
</body>
Since it is on the same domain you can get this to work but would have to change the way you were doing it a little.
First off you would have to open it in a popup using this syntax rather than a new tab:
newwindow=window.open(url,'name','height=200,width=150');
and then you could simply call newwindow.clicked() after the popup is called.
update
just did a quick test and this will open it in a new tab. (sorry its been a while since I used the open function.
newwindow=window.open(url,'name');
Just noticed also that you should wait for the popup to load. So in my Example it would look a little something like this (with jQuery):
var newwindow = window.open('http://www.tylerbiscoe.com/vb/new.html');
$(newwindow).load(function(){
newwindow.clicked();
});
Ok, brand new answer. I hope this is what you were thinking. This is however, when you open page 2 from page 1.. So basically, page 1 would know who page 2 is..
Online example: http://kopli.pri.ee/stackoverflow/6832271.php
Page 1
<html>
<head>
<title>Page 1</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style>
.ajaxlink {color: blue; cursor: pointer; border-bottom: 1px dotted blue;}
</style>
</head>
<body>
<span id="open_page_2" class="ajaxlink">Open new window</span>
<br>
<br>
Click Me!
<script>
$('#open_page_2').click(function(){
child = window.open('test2.php','page_2','width=600,height=600');
});
$('a[target=my_target]').click(function () {
child.SecondPageFunction();
return false;
});
</script>
</body>
</html>
Page 2
<html>
<head>
<title>Page 2</title>
</head>
<body>
<h1>Your seeing page 2!</h1>
<script>
function SecondPageFunction () {
alert('Second page action got triggered!');
}
</script>
</body>
</html>
The script must be a part of the page you're opening in the new window. You're absolutely correct about it being a security flaw if it was elsewise allowed.
You could add some query string argument that could be picked up onload by javascript in the page you are opening and call your function if the query string arg is present.