Javascript window.location.hash is defined? - javascript

Is there some way I can tell whether window.location.hash is defined?
If it is set, then I will be getting the value of the variable, and using it to display extra content on the page.

if(window.location.hash) {
// do stuff!
}

How about:
if(window.location.hash !== '')
{
}
Or
if(typeof window.location.hash !== 'undefined')
{
//your code
}

Related

How can i listen to Prestashop events in custom.js?

In the compiled theme.js i can find event listeners like prestashop.on("updatedProduct") and prestashop.on("clickQuickView").
More about those here.
I cannot get the web pack to work, so i would like to just add a listener in the custom.js (i know this is bad practice, i just need a quick solution).
How do i refer to this prestashop in custom.js, so i can listen for an event like updateProductList?
Here is a bulletproof answer :
$(document).ready(function () {
if(typeof prestashop !== 'undefined') {
prestashop.on(
'updateCart',
function (event) {
if(typeof event.reason.linkAction !== "undefined" && event.reason.linkAction == "add-to-cart") {
if (typeof event.reason.idProduct == "undefined" || event.reason.idProduct == "undefined") {
// Bulletproofed action
}
}
}
);
}
});

How to check if a specific link has been clicked(JS)

I'm making a chrome extension and what I want to happen is to alert the user if a specific link has been clicked on Facebook. And in my script every time I click a link it always alert accessing gma.
$(document).on("click", "a", function() {
//this == the link that was clicked
var href = $(this).attr("href");
if (window.location.protocol == 'https:'){
if(href == "gmanetwork"){
alert("Accessing Gma");
}}
else{
alert("false");
}
});
I would suggest something like this:
You change your html links to :
Your link
and your script:
$(document).on("click", "a", function() {
if($(this).attr("targetLink"){
alert("You are going to the following page: " + $(this).attr("targetLink"));
}
});
Code at Question assigns the value if (window.location.protocol = 'https:'){ "gmanetwork" to href using = operator at
if (href = "gmanetwork")
instead of checking the values for equality, use === operator
if (window.location.protocol === 'https:') {
if (href === "gmanetwork") {
alert("Accessing Gma");
}
}

Rewrite URL on location change

I'm looking for a way to rewrite URL of the location when the user want's to change page. So, let's say you have something like this:
<body>
<a href="http://example.com" />
</body>
Is there a way I can catch URL changing moment, and actually modify that URL before location is changed, for example I would like to change href into relative link like \http://example.com and redirect page actually there.
If you just want to trap the link and then modify it then yes, that's quite simple...
$("a").on("click", function(e) {
e.preventDefault(); // stops the link doing its default thing
window.location.href = "something/" + $(this).attr("href");
});
You obviously need to modify the line that changes the location, so that it modifies the href value however you need. I'd also recommend giving the links a class and selecting them with that, as the above code will affect every link on the page.
Finally, this will need to run after the DOM is loaded, so either wrap it in a document.ready handler of your choice, or put it in a script at the bottom of the body.
Demo
You can work from here. Also you will need urlrewrite in htaccess for this to work properly.
$(function () {
$('.buttonn').on('click', function (e) {
var seperator = (window.location.href.indexOf("?") === -1) ? "?" : "&";
if (window.location.href.indexOf("s1") === -1 && window.location.href.indexOf("s2") != -1) {
window.location.href = window.location.href.replace(/&?s2=([^&]$|[^&]*)/i, "&s1=s1");
} else if (window.location.href.indexOf("s1") != -1) {
window.location.href = window.location.href.replace(/&?s1=([^&]$|[^&]*)/i, "&s1=s1");
} else {
window.location.href = window.location.href + seperator + "s1=s1";
}
});
});
$(function () {
$('.buttono').on('click', function (e) {
var seperator = (window.location.href.indexOf("?") === -1) ? "?" : "&";
if (window.location.href.indexOf("s2") === -1 && window.location.href.indexOf("s1") != -1) {
window.location.href = window.location.href.replace(/&?s1=([^&]$|[^&]*)/i, "&s2=s2");
} else if (window.location.href.indexOf("s2") != -1) {
window.location.href = window.location.href.replace(/&?s2=([^&]$|[^&]*)/i, "&s2=s2");
} else {
window.location.href = window.location.href + seperator + "s2=s2";
}
});
});

If URL contains anything, then fire a Javascript function

Basically, if the URL/window.location contains absolutely any variable whatsoever (past domain.com/, of course), I'd like javascript to execute something.
Currently, I have the following jQuery code which only executes when window.location contains the exact wording "#hash", but as stated before I'd like to expand the functionality for all variables.
Edit: Sorry, to clarify, by variable I mean any one of the following examples:
domain.com/#hash
domain.com/#hash2
domain.com/sub/folder
domain.com/textwithoutahash
Also, if someone knows how to do this in basic Javascript and without the need for the jQuery library, that would be an added bonus!
$(function() {
if ( window.location.href.indexOf('#hash') > -1 ) {
myfunctionhere;
}
});
See update at end re your clarification
Put the script at the end of the page, just before the closing </body>, and:
If by "variable" you mean a document fragment identifier ("hash"), then:
<script>
if (location.hash) {
callYourFunction();
}
</script>
If by "variable" you mean a query string, then
<script>
if (location.search) {
callYourFunction();
}
</script>
If by "variable" you mean a resource name, e.g., not http://domain.com but http://domain.com/page, then:
<script>
if (location.pathname && location.pathname !== "/") {
callYourFunction();
}
</script>
More on the location object on MDN.
Re your clarification:
Edit: Sorry, to clarify, by variable I mean any one of the following examples:
Those examples come down to having either hash or pathname or both, so:
<script>
if ((location.pathname && location.pathname !== "/") || location.hash) {
    callYourFunction();
}
</script>
...and of course, if you also wanted to handle http://domain.com?foo=bar, then add in search as well:
<script>
if ((location.pathname && location.pathname !== "/") ||
location.search ||
location.hash) {
    callYourFunction();
}
</script>
You could check if there is a hash, a pathname or a search.
Or, to simplify, you could simply use this:
if (window.location.href.split('/').filter(Boolean).length > 2) {
callYourFunction();
}
window.location.href is simply the whole URL. If there's something after the domain, it'll be shown.
This function will be triggered for the following cases:
domain.com/some/path
domain.com/#hash
domain.com/?some=variable
You could check if search property of window.location is set to something. Also, you can check the hash property:
if (window.location.search || window.location.hash) {
yourfunctionhere();
}
To invoke it without jQuery, just include it in an 'onload' script:
<script type='text/javascript'>
document.onload = function () {
if (window.location.search || window.location.hash) {
yourfunctionhere();
}
}
</script>

Jquery hashchange problems in firefox

I am having a problem with the hashchange event in Firefox. We are using the JQuery hashchange plugin provided by Ben Alman. The code is as follows.
$(window).hashchange(function (e) {
alert("Hello");
//we want to perform a post in here.
});
var temp = "#123";
if (temp !== "") {
if (window.location.hash == temp) {
$(window).hashchange();
}
else{
window.location.hash = temp;
}
}
else {
window.location.hash = "#Home/Home";
};
Now this works fine in IE9 and Chrome, however in Firefox, I see the alert, but as soon as I click OK, the page refreshes, displays the alert again, and continues infinitely. Is there some sort of weird behaviour that Firefox uses that I am unaware of? Or is there simply some other problem that is hidden deeper?
In some browsers window.location.hash includes the # and in some don't so its better if your ignore it while comparing the hash value in your code.
Try this.
$(window).hashchange(function (e) {
alert("Hello");
//we want to perform a post in here.
});
//Remove hash from here which will be compared with window.location.hash
var temp = "123";
if (temp !== "") {
//Replace # by empty nothing
if (window.location.hash.replace('#', '') == temp) {
$(window).hashchange();
}
else{
window.location.hash = '#' + temp;//Now add the hash here
}
}
else {
window.location.hash = "#Home/Home";
};
We located the problem as occuring in MicrosoftAjax.js and found the following solution:
Firefox 6 Infinite Page Refresh With Page With Hash Tags

Categories