I have to submit a form which opens next page, but URL in next page has to be #/indes/string.
I have a page with input box and button. I already have logic to open new page, transfer parameter via:
var inputStr = document.getElementById('input_box').value;
var w = window.open("indes.html", '_blank');
w.input = inputStr;
and show it on new page with
<script> document.write(input);</script>
But I have to input this "string" to URL so the URL looks like:
#/indes/string
My URL should look like this at the end: www.mydomain.com/#/indes/string
How can I do this? Is this URL-rewriting? Can I even use this on my WAMP?
Check out sammy
Seems just like the thing you are looking for.
I think you need to set the Location Hash Property by setting window.location.hash.
instead of open a new window use location.hash but make sure you prevent default behavior by return false or if you use jquery use preventdefault.
function() {
[your logic]
location.hash = "#/indes/string";
return false
}
Related
I have a select item with bunch of cities on my website. When the visitor selects some city this happens:
$("#city-selector").change(function() {
var url = $(this).val(); // get selected value
if (url) { // require a URL
document.location.href = url; // redirect
}
});
Each option inside the select has a value parameter that contains subdomain url in it.
The problem is document.location.href doesn't behave like a simple link. It clears visitor id and it looks like the visitor doesn't have a referer and came to the new subdomain out of nowhere. Is there a problem with functions I use or should I dig into crossdomain sessions/cookies? How do I make it behave properly?
Use window.location.href. This is similar when clicking a link
window.location.href = url;
I need to get the previous url to redirect to the previous page. I have url like www.mysite.com/users/register/#1.
I use document.referrer to get the previous url,but it doesn't return hash part(#1). How to get the previous url including hash part?
How to get previous url including hash fragment using JavaScript?
As you've noted, the hash fragment part of that means you can't use document.referrer.
If the previous page was on the same origin: You'd need to have code on that page recording the full URL, for instance in sessionStorage.
On the previous page, perhaps each time hashChange is fired:
sessionStorage.setItem("last-url", location);
On the new page, to get the URL:
var lastUrl = sessionStorage.getItem("last-url");
If the previous page was on a different origin: I'm fairly certain you can't.
I need to get the previous url to redirect to the previous page.
Actually, you don't. You can just use history.go(-1) or history.back() to do that, which work regardless of the origin of the previous page.
try for previous url,
function backtopage() {
window.history.back();
}
May be you can use onhashchange event.
When url is changed,it produces a event with old url and new url.
The oldurl has even the hash part
$(window).bind('statechange',function(){
// Prepare Variables
var State = History.getState(),
url = State.url,
states = History.savedStates,
prevUrlIndex = states.length - 2,
prevUrl = states[prevUrlIndex].hash;
});
Try this one::
In previous page url:
www.mysite.com/users/register/#1
In Current Page:
$(document).ready(function() {
var referrerUrl = document.referrer.replace("#","e");
var correctUrl=referrerUrl.replace("e","#");
});
function updateView(category) {
console.log( window.location.hash );
if (location.hash !== ""){
//convert #3 to 3.
//load video based on id
//myArray[sanitizedHash];
} else {
updateCategoryLabel(category);
currentList = updateVideosList(category);
chooseRandomVideoFromList();
}
}
This function is loaded on page load
How can I parse inside this function so that the the location.hash's '#' will be taken out of the URL?
In short I am trying to achieve www.mysite.com/3 versus www.mysite.com/#3
Thanks in advance!
Edit: I should add that the 'else' is basically randomizing on page load versus going to the direct url. This if statement will run on page load to check if the hash exists otherwise it will randomize as usual.
Altering the URL from 'www.mysite.com/#3' to 'www.mysite.com/3' will cause the browser to navigate to a new URL since www.mysite.com/3 is not the same page as www.mysite.com/#whatever.
If you just want a string with the first character of the hash trimmed try:
window.location.hash.substr(1)
You can get the window.location.hash and then replace the # with an empty string
if (location.hash !== ""){
var sanitizedHash = window.location.hash.replace("#", "");
//load video based on id
//myArray[sanitizedHash];
}
If your goal is NOT to trigger page load, you can use HTML5 History API to change URL from www.mysite.com/#3 to www.mysite.com/3 like that:
var id = location.hash.substr(1);
history.replaceState({id:id}, id, id);
Note that replaceState is used, because otherwise user can press back button to the browser and get back to the #3. If you want to allow that, replace replaceState with pushState. Hope that helps.
The situation is that I have links:
Edit Account Info - that points to http://example.com/user/edit
I setup a javascript that will only fetch information in the controller then will load it in a template.
I have this code that does the eventhandling
$("#lnkEditUser").on("click",
function()
{
some.settings.edit_profile();
return false;
})
What it does, is that it replaces the content of a div with the template,
so I have it loaded, the template and the details from the controller(from the model) via ajax calls. The problem is that when I try to refresh the page, the content would be its default. Is it possible that when I click on the link the URL will then change to something like:
http://example.com/user#edit
So even when I refresh the page, the content loaded via ajax will be the same?
If I remove the #edit, it'll be the default?
Correct me if i did this right, or if there is a better way to do it..
first I check if the hash exist, then get the value of the hash, assigned it to variable hash then i removed the hash, maybe i can do also do this replace("#", ""). I realize other stuffs while i'm typing this like i should have been converted it hash to string right on the assigning of value. But anyhow, this is how I did it.
activeTemplateViaHash : function() {
if (window.location.hash) {
var hash = window.location.hash;
hash = hash.toString().substr(1, hash.toString().length-1);
var method = new Function('some.settings.' + hash + '()');
method();
}
},
Try doing this:
activeTemplateViaHash : function() {
if (window.location.hash) {
var hash = window.location.hash;
hash = hash.replace("#", "");
var method = some.settings[hash]; //find the hash method
if(method) { //if the method exists then run it
method();
}
else {
alert(hash + " function doesn't exist!");
}
}
},
But instead of relying on hashes like that, I would use localStorage or COOKIES to store user settings easily.
I have the following code below in a javascript file and need to have the link that is being generated open in a new window.
if (currentSearchType === 'extSearch') {
extSearchSearchValue = extSearchSearchInput.val();
window.location.href = replaceByObject(global.uhg.data['general'].body.extSearchSearchUrl, {
q: extSearchSearchValue
});
Normally with javascript I believe you'd use a window.open type of function, but not sure how to incorporate that with this type of code.
However you do it, opening a new browser window with javascript will most probably be blocked by popup blockers, so perhaps you should rethink your approach to the user himself clicking a regular link, then you can use target="...".
Just use a var to hold the URL and then pass it to window.open()...
if (currentSearchType === 'extSearch') {
extSearchSearchValue = extSearchSearchInput.val();
var url = replaceByObject(global.uhg.data['general'].body.extSearchSearchUrl, {
q: extSearchSearchValue
});
window.open(url, 'searchWindow');
}