Javascript/jQuery hash URL doesn't execute - javascript

I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
$(function($){
var key=$(location).attr('hash');
if (key){
$('#div').find('div.div2').load('sig.php?k='+key)
}
});
This works fine when I type in the URL manually or refresh the page but whenever I use <a href="url#SecReTkEy"> or Javascript/jQuery functions such as:
$(location).attr('href','url#SecReTkEy');
$('a').prop('href','url#SecReTkEy');
window.location.replace('url#SecReTkEy');
window.location.href('url#SecReTkEy');
Can any one tell me why and how to fix this?
`

you should use
location.href = #whatever

You don't need JQuery in order to retrieve the hash. Use the fully qualified reference to window.location.hash (I'm guessing location didn't work as you already have a var named location).
Also, don't forget to URL encode the key using encodeURIComponent. Additionally the $ is not needed if you want to execute this on document ready.
$(function(){
var key = window.location.hash;
if (key){
$('#div').find('div.div2').load('sig.php?k=' + encodeURIComponent(key))
}
});

I have a hash URL that doesn't execute the javascript/jQuery unless I manually hit the enter key or refresh the page.
This was due to it only launching once rather than having an eventBinder on hashchange.

Related

Javascript page refresh to base URL, stripping query string, on browser resize -

I'm using this piece of javascript code to refresh the browser when the user resizes it:
<script type="text/javascript">
$(window).bind('resize', function(e)
{
if (window.RT) clearTimeout(window.RT);
window.RT = setTimeout(function()
{
this.location.reload(true); /* false to get page from cache */
}, 200);
});
</script>
It works fine, except - the page I'm using has a query string with certain values, like:
http://www.example.com/index.php?w=123&h=456
What I'd like is the same functionality but to have the page refresh to the URL without the query string (or at least without its values).
Is that possible?
since location is an object ,you can change the property 'href' to whatever you like , for example you can change the new location and assign it to the href property:
your current location is : http://www.example.com/index.php?w=123&h=456
when you need to refresh the page without the query string, you can do this: window.location.href = "http://www.example.com/index.php"
hope helps,
good luck
Try using window.location = window.location.pathname instead.
If you to redirect the page to root domain, then use window.location.origin. If you want to include the script name too then use window.location.origin and window.location.pathname. So your redirect can be replace with
window.location = window.location.origin + window.location.pathname

How do I return part of the url using window.location.hash

I am really stuck
I am currently using the code below to add the name of a dynamically loaded page to the url
var value = $(this).attr('href');
e.preventDefault();
window.location.hash = value;
it almost works as its returning this in the url
http://www.sitename.com/dev888/#http://www.sitename.com/dev888/page-name
But I the only want part of the url to return like the example below
http://www.sitename.com/dev888/#/page-name
How can I edit the code above to get my desired result?
Thank!!
You can play with the value to get the last one like this
var value = $(this).attr('href');
e.preventDefault();
var parts = value.split("/");
value = parts[parts.length-1];
window.location.hash = value;
Or you can change the attr('href') to the value you want directly :)
I hope this can help
And you may want to take a look at pusState
https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history
The browser already does this for you. Just make your link's href the hash, and let the user click it normally:
Click!
It will automatically append #my-hash to the URL.
Trying to accomplish this with JavaScript is totally wrong, unless you require logic to prevent the click under certain conditions, and even then the correct behaviour is to specify your hash as the href of the link, conditionally allowing the click event to be handled by the browser.
Maybe you find interesting to obtain the title of the page and use it in the hash
var current_title = $(document).attr('title');
e.preventDefault();
window.location.hash = current_title ;

how to refresh web page using javascript?

I want to refresh a web page using javascript and, I knew how to do it but I have one problem,
before refresh:
url: http://www.example.com/index.html#introduction
after refreshed:
url: http://www.example.com/index.html
I want to use that hash as parameter in another javascript function after page has refreshed.
So, how can I save that hash after the page is reloaded?
I belive you have implemented refresh using location.reload() method, instead try redirecting to the same page using location.href like below.
location.href = "http://example.com/index.html#introduction"
This will refresh the page and also maintains # in url.
As commented, if u use only static code then u need to use cookies(if also support legacy browsers) else u can use localstorage(for modern browsers) for storing the hashValue.
to reload without hashvalue use the following code
window.location.href = window.location.href.split("#")[0];
before calling the above code u need to save the hashValue.
var hash = location.hash.substr(1);
localStorage["hashValue"] = hash;
retrive the above value later as
var hash = localStorage["hashValue"];
Try
escape(window.location.href)
if you want to add parameters:
for example
escape(window.location.href="parameter="+param);
try this
window.location.href = window.location

Window.Location Refreshes instead of Redirects

I have a JQUERY function as follows
this.getURL = function()
{
var name = getName();
alert("Menu.aspx?name"+name);
//window.location = "Menu.aspx?name"+name;
}
When I alert the URL I am attempting to go to, it is correct. However, when I call window.location on that string, the page just refreshes without going anywhere.
I have similar code where I have used window.location and it works. I typed in the url into my browser and it works as well.
At worst (even if the URL was wrong), I was hoping that it would just redirect me to some URL. However, I can't get it to do anything other than refresh the current page.
Also to clarify, the page which calls this function is not Menu.aspx
Thanks in advance.
If you're using a relative path try setting window.location.pathname, otherwise set window.location.href for a full path.
You may also want to try self.location.href
In my experience, it's been difficult to get redirects like this to work right. I've had to use window.location.replace(<url>). If you're just changing an anchor tag, it's even more difficult. You have to do the following to get it to work in all browsers:
window.location.replace(<url>);
window.location=<url>;
window.open(<url>,'_self');
window.location.reload();

Sending parameters via url during reload

I am doing an ajax request and in the success callback I want to refresh the page to reflect the changes. I am trying to pass parameters to the same page as I refresh that page.
The url is something like:
http://localhost:8080/details#component/12 to which I am appending the parameter as said below.
window.location.href += "?code=approved";
window.location.reload();
The above works in Firefox but not in IE, can you please help here?
Chris.
Try using these for IE:
window.location.reload(false); // To get page from cache
window.location.reload(true); // To get page from server
The hash is the problem; you append your data to the URL fragment (part after the #) The fragment isn't send to the server, ie the url doesn't change so no need to request the page again. Try manually adding '#some_text' to the url in your browser and see what happens ;)
Try something like this:
var newloc = document.location.href;
if(document.location.hash){
// remove fragment
newloc = newloc.substr(0, newloc.indexOf(document.location.hash));
}
newloc += document.location.search ? ";" : "?"; // prevent double question mark
newloc += 'code=approved';
// append fragment back to url
if window.location.hash is empty, you cant assign to location.href a new value without using a correct function (at least tested in chrome).
try the window.location.replace:
if (!window.location.hash)
{
window.location.replace(window.location.href + "?code=approved")
}

Categories

Resources