1

I'm new to this website so I'm sorry if I do something wrong, please tell me if I do so.

Problem: I'm trying to get a href from the current page I'm on with a button like "Get Href" and when it manages to get a href it will display text Href gotten or if it fails it says Href not found. If it does find it a new button appears which says Goto Href and that takes the user to the href. I've been searching the whole week for a solution but so far I have no luck. Also this is a chrome extension.

Useful Links Found So Far:

I am new to HTML & jQuery & CSS & Javascript so I apologize for my lack of knowledge in advance. If you are able please explain your answers in detail. If answer is found please comment the functions out so I could manually research them further.

I don't know where to start, what I have so far is what the Getting Started: Building a Chrome Extension has.

----UPDATE .1----

I thank you for your replies and what I've gathered so far from them is this code

<script>
$(document).ready(function(){
  $("button").click(function(){
    alert($('a').attr('href'));
  });
});
</script>
</head>
<body>

<button>Find Href</button>
</body>

but nothing seems to happen. Also instead of alert($('a').attr('href')); I'm trying to put $('a[href^="magnet"]') This code is gathered from different websites so I don't know if it is legit (still working) or not.

Community
  • 1
  • 1
  • 1
    If you want to get the URL of a page then use: document.location –  Jun 14 '14 at 23:45
  • 1
    *"I'm trying to get a href from the current page [...] a new button appears which says Goto Href and that takes the user to the href"* ... so you want to create a button that reloads the page? *"what I have so far is what the Getting Started: Building a Chrome Extension has."* Are you actually building a Chrome extension? – Felix Kling Jun 14 '14 at 23:49
  • @Felix Kling Yes, I'm actually making it and as you can see I'm not doing any good :/ – TwilightKillerX Jun 15 '14 at 09:01
  • Where is the href? Is it on the page or you are trying to get the current page url? Can you post your code on jsfiddle? – Rajarshi Goswami Jun 15 '14 at 09:27
  • The href is located in the current page source. My code is quite empty and the main parts of it are above in the UPDATE .1 – TwilightKillerX Jun 18 '14 at 19:29

2 Answers2

0

try this javascript:

document.location.href

I type it in the console and it returns:

http://stackoverflow.com/questions/24225281/html-jquery-get-href 

, which is current link.

Mr.Cocococo
  • 1,401
  • 2
  • 11
  • 13
0

Before you start, you should read some javascript tutorials. Go search the internet for "javascript tutorial" (...please... do it). If you fail on this task, you will fail on others. This is not helpfull for you or others.

What do you want to do with "href" and why do you search only for chrome answers rather than search for a javascript answer.

Anyway, if you open the console you can test this by yourself.

alert(location) <-- shows you the current location of the browser

or every href of every a tag (in jquery).

alert($('a').attr('href')); <-- a lot of links ;)
Raxa
  • 372
  • 3
  • 7