URL check only works on second time within iframe - javascript

I currently use
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if (window.location.href.indexOf("Startseiteplayerrun") > -1) {
alert("You have the player open already !");
window.history.back();
} else {
//window.open("Startseiteplayerrun.html","_self");
link = document.createElement("a");
link.href = "Startseiteplayerrun.html";
//link.target = "_blank";
link.click()
;}});
</script><br>
home
to check whether the string "Startseiteplayerrun" is present in the url bar.
However,
this check only seems to happen after failing the first time
=> I'm able to open the frame once within itself:
Can this be mitigated ?
The purposeof this "if else" is to not allow another iframe,
if the urlbar contains "Startseiteplayerrun" ...

If the string won't check right away, check if framed
~FIN~
<script>
if ( window.location !== window.parent.location ) {
alert("You have the player open already !");
window.history.back();
} else {
// The page is not in an iframe
}
</script>

Related

How to capture the referring URL in a variable and then redirect to new page jquery or javascript

Almost have it, but might need a hand :
I created a script to redirect AND set a cookie that comes in from ANY page from our site. So even if a user comes in from a direct link to a news article, it will redirect them to our "splash" page and set a "cookie" to avoid further redirects. I have that part working perfect.
$(document).ready(function() {
if (typeof Cookies.get('secondvisit') === 'undefined') {
window.location.href = "/index-donate.php";
}
})
However, we now want to capture the URL that they came to first and create a variable so we can have them be linked back to that page after they read our SPLASH page.
So in the example above:
Come in via direct link: /news/article1.php
No cookie is detected, so we first need to "capture" the page they came in on "$page-refer" and then redirect them to our Splash page.
On the splash page, we would then present a LINK to them with "Proceed to webpage" with the "$page-refer" link.
I did try this ( BELOW ), but this is only grabbing the "google" page and not OUR webpage that they hit first.
Thanks!
$(document).ready(function() {
if (typeof Cookies.get('secondvisit') === 'undefined') {
var referringURL = document.referrer;
var local = referringURL.substring(referringURL.indexOf("?"), referringURL.length);
location.href = "/index-donate.php" + local;
}
})
I think you could add the URL as a cookie when you do the redirect, ex:
$(document).ready(function () {
if (typeof Cookies.get('secondvisit') === 'undefined') {
Cookies.set('initialvisitedpage', window.location.href);
window.location.href = "/index-donate.php";
}
});
then you could redirect them to the cookie value:
$(document).ready(function() {
if (typeof Cookies.get('secondvisit') === 'undefined') {
window.location.href = Cookies.get('initialvisitedpage') || '/'; // or whatever this could default to in case they don't have the cookie
}
})
Put together an answer that works great, thanks for the comments and push:
On footer script template:
$(document).ready(function () {
if (typeof Cookies.get('secondvisit') === 'undefined') {
location.href = "/index-donate.php?page-refer=" + encodeURIComponent(location.href);
}
})
Then on my Splash page:
I captured the variable $pageredirect via $_GET['page-refer'] and then presented that as a link further down the page ( if set )!

Want to refresh page one time on particular URL hit

I am trying to refresh the page only one time when a particular URL is hit.
The reason for this is to refresh the data on the view. My URL is
http://localhost:8072/web?#min=1&limit=80&view_type=list&model=pos.order&action=405
And solutions I have tried are
window.onload = function() {
//considering there aren't any hashes in the urls already
if(!window.location.hash) {
//setting window location
window.location = window.location + '#loaded';
//using reload() method to reload web page
window.location.reload();
}
}
(function()
{
if( window.localStorage )
{
//check if reloaded once already
if( !localStorage.getItem('firstLoad') )
{
//if not reloaded once, then set firstload to true
localStorage = true;
//reload the webpage using reload() method
window.location.reload();
}
else
localStorage.removeItem('firstLoad');
}
})();
$(document).ready(function(){
//Check if the current URL contains '# or hash'
if(document.URL.indexOf("#")==-1){
// Set the URL to whatever it was plus "#loaded".
url = document.URL+"#loaded";
location = "#loaded";
//Reload the page using reload() method
location.reload(true);
}
});
None of them worked for me. What is wrong or which direction should I choose?
Your first solution works fine, it just needed a return statement to prevent execution the rest of the code, however if your original url contains a hash, you can do the check like this:
window.onload = function() {
document.getElementById("test").textContent = window.location;
console.log(window.location.hash);
//check for our specific hash argument
if (!window.location.hash.match(/[#&]loaded(&|$)/)) {
alert("the page is about to refresh");
//setting window location
window.location = window.location + (window.location.hash ? "&" : "#") + 'loaded';
//using reload() method to reload web page
window.location.reload();
return;
}
alert("page refreshed");
}
<div id="test"></div>

How to check window.location exist or not

My current code is working when location is found. But I want to make a check that if location is not found, it will redirect somewhere else.
Here is my code:
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
window.location = "theMYApp://"
}
else
{
}
</script>
I want to check if window.location = "theMYApp://" does not exist
If you want to check whether window.location object is existed on a web browser, you do not check. window.location object is always existed.
If you want to check text of 'theMYApp://' in window.location object, you can use below codes.
if (window.location.indexOf('theMYApp://') > -1) {
// theMYApp:// string is found in url
}

How to restore original content after window.popstate?

The following code goes on pushing a new URL to the state object, while dynamically changinf the page's content as well. However, when I start pressing the Back button and return to the original page, the original content is not shown, instead the next page's content is retained. How do I achieve it?
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
a = 0;
$("p").click(function(){
var stateObj = { note : ++a };
history.pushState(stateObj, "page 2", "http://localhost/foo/"+a);
$(this).text(a);
});
window.addEventListener('popstate', function(e){
if (history.state){
$("p").text(e.state.note);
if (location.href == 'http://localhost/hist.php') { $('p').text('If you click on me, I will disappear.'); }
}
}, false);
$("div").click(function() { alert(e.state.note); });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<div>Hi</div>
</body>
</html>
It is your responsability to update the page content on popstate. The browser only handles the state.
I had an application, where I replace the main content of a page while navigation through an menu tree. I also updated the page title according to the new content.
When I just go back an forth in AJAX loaded pages, I can save the url to load in the StateObj. But on going back to the initial page, there is no state, and no saved title to restore.
I decided to reload the whole page, when I go back to the initial history state:
var doc_state={'title': ''};
var popped = ('state' in window.history && window.history.state !== null), initialURL = location.href;
function loadDocument(path,title){
$('#document_area').html('<img src="spinner.gif" />');
$('#document_area').load(path+'?ajax=true');
document.title = title;
}
$(document).ready(function(){
$('a.ajax_link').click(function(event){
var path=$(this).attr('href');
var title=$(this).text();
doc_state['title']=title;
event.preventDefault();
loadDocument(path,title);
window.history.pushState(doc_state,document.title,path);
});
$(window).bind('popstate', function(event){
// Ignore inital popstate that some browsers fire on page load
var initialPop = !popped && location.href == initialURL;
popped = true;
if ( initialPop ) return;
var state = event.state;
if (!state){
state=history.state; // FF
}
if (state){
var title= state.title;
loadDocument(location.pathname, title);
}
else window.location.reload();
});
}

How do I automatically remove an HTML page frame?

What's the best way to remove a page frame automatically?
I've used this type of code before:
<script language="JavaScript">
setTimeout ("changePage()", 3000);
function changePage() {
if (self.parent.frames.length != 0)
self.parent.location="http://www.example.com";
}
</script>
Do you mean if someone has put a frame around your content? If so, you need the following any where in your HTML page to jump out of the iframe:
<script type="text/javascript">
if (window.top.location != window.location) {
window.top.location = window.location;
}
</script>
Here's an alternative that's more generic in that it doesn't name the parent URL, nor use the separate function call:
// is the current page at the top of the browser window hierarchy?
if (top.location != self.location)
{
// it isn't, so force this page to be at
// the top of the hierarchy, in its own window
top.location = self.location
}
Do it this way if you want the frame-breaking step to not appear in the history
if ( self.location !== top.location )
{
top.location.replace( self.location );
}

Categories