avascript:history.go(-window.history.length) does not work - javascript

I am doing mobile html page via phonegap
I want to redirect user to first page. So I cannot give direct link such as a href to index.html
When User click logo I want to calculate page length that he visited and use history.go.
function goBack() {
var backLength=window.history.length;
javascript:history.go(-backLength);
}
but this does not work, no error but don't redirect

Remove javascript: and wrap in script tag:
<script>
function goBack() {
var backLength=window.history.length;
history.go(-backLength);
}
</script>

Clean up code. To avoid submit, use (javascript: void 0) in href. Use proper callback
function goBack() {
console.log("Test")
var backLength= window.history.length;
history.go(-backLength);
}

I fixed it by using:
window.history.length-1

Related

Onclick auto popup code

I want to know that how can i create a JavaScript code that must open a new page at click anywhere at website. currently i am using onload script.
<script type="text/javascript">
function open_on_entrance(url,name)
{
window.open('http://realestatepakistan.pk','Real Estate Pakistan')
}
</script>
<body onload="open_on_entrance()"></body
but onload is not effective as it opens very low number of pages. A must must open page script needed fro my web http://pkr.com.pk/.
This code will open the popup when you click anywhere on website.
<script type="text/javascript">
var shouldOpenWindow = true;
function open_on_click(url,name) {
if (shouldOpenWindow) {
window.open('http://realestatepakistan.pk','Real Estate Pakistan');
// if you want that only on first click the popup must be opened, and not on any subsequent clicks, then do this
shouldOpenWindow = !shouldOpenWindow;
}
}
</script>
<body onclick="open_on_click()"></body>
Place this code in a script tag right before your closing </body> tag that way it will load after the dom has loaded. You will notice that I removed your parameters from your function because you were not using them inside of your script. Make sure you change the values of window.open to the values you need for your script.
JavaScript:(live example)
function open_on_entrance() {
window.open('http://bytewarestudios.com','Welcome to Byteware Studios');
}
document.addEventListener("click", function() {
open_on_entrance();
});
If you decide to the add the parameters back in make sure you supply them to your function when you call it.
Example Using Parameters:
function open_on_entrance(url,name) {
window.open(url,name);
}
document.addEventListener("click", function() {
var url = 'http://bytewarestudios.com';
var name = 'Welcome to Byteware Studios';
open_on_entrance(url,name);
});

If user came from previous page on site then this, else do this

What would be a viable way to accomplish the following:
A website has two pages; Parent page and Inside page. If user came to the Inside page directly by typing in the address or by following a link from a page other than Parent page, then show "foo". If user came to the Inside page from the parent page, then show "bar".
I would need this done in JS if possible. If not, PHP is a secondary choice.
You can get the page the user came from with document.referrer.
So you could implement your solution like this:
if (document.referrer === 'yoursite.com/parentpage') {
// do bar
} else {
// do foo
}
Please try this
This code in second page
jQuery(window).load(function() {
if (sessionStorage.getItem('dontLoad') == null) {
//show bar
}
else{
//show foo
}
});
This code in parent page
jQuery(window).load(function() {
sessionStorage.setItem('dontLoad','true')
});
with php:
There is a simple way is to create a mediator page which redirect to inner page after make a session / cookie.. then if you'll get session / cookie, you show foo & unset session.
if someone directly come from url, no session / cookie found & it show bar..
You can use the document.referrer but this is not always set. You could add a parameter to the URL on the parent page and then check for its existance in the child page
Link on the parent page:
<a href='myChildPage.html?fromParent=1'>My Child Page</a>
JS code on your child page:
var fromParent=false;
var Qs = location.search.substring(1);
var pairs = Qs.split("&");
for(var i = 0; i < pairs.length; i++){
var pos = pairs[i].indexOf('=');
if(pos!==-1){
var paramName = pairs[i].substring(0,pos);
if(paramName==='fromParent'){
fromParent=true;
break;
}
}
}
if(fromParent){
alert("From Parent");
}else{
alert("NOT From Parent");
}
This method isnt 100% foolproof either as users could type in the same URL as your parent page link. For better accuracy check the document.referrer first and if not set use the method i've outlined above
intelligent rendering with jQuery
After using #Rino Raj answer, i noticed it needed improvement.
In javascript, the load() or onload() event is most times much slower,
since it waits for all content and images to load before executing your attached functions.
While an event attached to jQuery’s ready() event is executed as soon as the DOM is fully loaded, or all markup content, JavaScript and CSS, but not images.
Let me explain this basing, on code.
When i used #Rino Raj's code, with load() event, it works but on the second/called page, the content appears before class="hide fade" is added (which I don't really want).
Then i refactored the code, using the ready() event, and yes,
the content that i intended to hide/fade doesn't appear at all.
Follow the code, below, to grasp the concept.
<!-- Parent/caller page -->
<script type="text/javascript">
$(document).ready(function() {
sessionStorage.setItem('dontLoad', 'true');
});
</script>
<!-- Second/called page -->
<script type="text/javascript">
$(document).ready(function() {
if(sessionStorage.getItem('dontLoad') == null) {
$("#more--content").removeClass("hide fade");
} else {
$("#more--content").addClass("hide fade");
}
});
</script>

Call javascript function on passing through link

I have a CMS which loads articles using ajax. The article is loaded through a function with some parameters. What I need is: when someone clicks a certain link, it will redirect him to the target page AND launch the function on target page. Is it possible? To be concrete, I have function loadArticle(articleID). When I access the page, there is article list. When I launch function loadArticle, it hides div with article list and shows particular article. So I need some way to call it through link, like: <a onArticlePageLoad="loadArticle(15)" href="./articles">Title</a>
Example: on page.html I have a link which points to page2.html. On click, I need to load page2.html and execute function foo in page2.html
As mentioned by ChristianF you ll need to have an identifier on your link. I have this function:
function hashtagExecute(hashtagstring,action){
if (location.hash==hashtagstring) {
action();
}
}
/*Gets: STRING -- Beggining with '#'
* FUNCTION -- The function to be executed if the above string is present in the url
Then on the link page2 with foo execution
and onload:
window.onload =function (){
hashtagExecute("#foo",foo());
hashtagExecute("#bar",bar());
... //as many different cases you want just remember to actually have those functions :)
}
You could just have a function that executes on page2.html at the bottom of the body or onload.
As people were already saying, you will need some sort of onload event on page2. But it sounds like you want that function being called to change depending on what link was clicked on page1.
Would something like this work:
page1:
page2 case A
page2 case B
page2:
<script type="text/javascript">
window.onload = function foo(){
var case = window.location.search;
if (case == "A") {
functionA();
} else {
functionB();
}
}
</script>

a href combi with javascript onClick

How do you make a javascript "onClick" and an html "a href" merge into one link? I want that when I click on the link the first step is the "onClick" and the second when the "onClick" is successed you redirected with the "a href" to another page.
First step: onClick="ajaxrequest('/includes/docx/boekenlijst.php', 'docgegevens')
and
Second step: a href="/Raymond_converters/docx/example.php">doc
I hope you can help me but my English is not so good. I'm sorry for that.
Using jQuery, you could try this:
<script type="text/javascript">
$(document).ready(function() {
var call_ajax = true;
$('#alink').click(function(e) {
if (call_ajax) {
e.preventDefault();
// Do AJAX
call_ajax = false;
}
}
</script>
google
It's not simple, because if you let the link do its default action i.e. redirect to its href, the AJAX request won't be triggered.
What you need to do is:
Handle the onclick and cancel the default action by having return false;
When the AJAX request is done (in its success callback method) manually redirect the user to the link href location.
Try to use jquery with a dummy callback:
<a onclick="function1()" >function</a>
function function1(someVariable,function() {
function2(someOtherVariable);
window.location="http://www.yourpage.com/";
})
function function2(someVariable, callback) {
...YUOR CODE HERE
callback();
}

Changing URL from an iFrame: setTimeout changes how window.location works?

My iframe contains a link that should change the hash/trigger an event in the parent.
How come this code won't work:
<!-- in iframe -->
Link
// in parent
function navigate() {
window.location.href = '#anchor';
}
But this does:
<!-- in iframe -->
Link
// in parent
function navigate() {
setTimeout(function() {
window.location.href = '#anchor';
}, 0);
}
I think you have an issue with the context that the navigate() function is in, with window being in the context of the iframe.
Try setting you navigate method to use top.location.href instead, for example:
// in parent
function navigate() {
top.location.href = '#anchor';
}
or alternatively, in your iframe, call parent.window.location.href = '#anchor'
#am not i am's comment that you have an apostrophe is likely the cause
Even the code highlighter is showing different highlights because of it.

Categories

Resources