0

I want it so that if someone visits:

http://www.site.com/#hash

It puts the data after the hash into the input box with the id url

and then it submits the form.

Here is my code:

<form method="post"> <input id="url" placeholder="Enter" name="url"> <input type="submit" id="visit" value="Visit" class="submit"> </form> 

How can I do this?

1
  • Caution - Differences between browsers! In response to location.hash, some will return '#hash' and others just 'hash'. Commented Jan 28, 2012 at 5:12

5 Answers 5

3

you can use

 if( window.location.hash) { var hashVal = window.location.hash.substring(1); $("#url").val(hashVal ); } else { alert("no hash found"); } 
Sign up to request clarification or add additional context in comments.

6 Comments

you will need to do if( hash.indexOf('#') == 0 ) { hash = hash.substring(0,hash.length); }
@Shaheer thanks alot for pointing that out, edited the answer
but you need to add substring only if '#' is present, some browsers wont have it, which will make your current code behave incorrectly.
Can't I just use hash = window.location.hash.replace('#', ''); ?
@Latox yes you can use that as well
|
1

Try something like:

$(document).ready(function(){ var hash = window.location.hash; $('#id').val(hash); $('form').submit(); }); 

Comments

1

window.location.hash will give you the hash value from the url. You can use this code.

//Instead of empty value set whatever you want in case hash is not present $("#url").val(window.location.hash || ""); 

Comments

0

try to google for jquery.ba-hashchange.min.js, it needs to be used with jQuery

$(window).hashchange(function(){ $("#url").val(window.location.hash); }); 

Comments

0

You can use this plugin to do it - https://github.com/marklar423/jquery.hash it's pretty straightforward.

Example:

//get var page = $.hash('page'); //set $.hash('page', 1); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.