Latox
Latox

Reputation: 4705

Grabbing hash from URL?

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?

Upvotes: 0

Views: 353

Answers (5)

Code Slinger
Code Slinger

Reputation: 1130

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);

Upvotes: 0

Rafay
Rafay

Reputation: 31043

you can use

    if( window.location.hash) {
         var hashVal = window.location.hash.substring(1);
          $("#url").val(hashVal );
      } else {
          alert("no hash found");
      }

Upvotes: 3

vicker313
vicker313

Reputation: 336

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

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

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

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 || "");

Upvotes: 1

redronin
redronin

Reputation: 723

Try something like:

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

Upvotes: 1

Related Questions