Bacon2305
Bacon2305

Reputation: 311

How to get URL within content with JQuery or Javascript

I am building an Application with PhoneGap which has a lot of URLs that are updated on a regular basis. Therefore I need to code a way to capture the URL and redirect it to the external browser. The InAppBrowser code is not the problem, I just can't seem to get the URL! Here is what I have tried:

$('#page-content a').click(function(){
   currentPage = this.href.split('=')[1];
  window.open('currentPage', '_blank', 'location=yes')
});

No luck with the above. For one, I think the issue started with the ('='), as I thought that would be where to begin the split of the href from the URL

Here is a small section of the content that holds the URL I need to get:

    RSVP: from website<br />
Web: <a href="http://sites.ieee.org/scv-cas/">sites.ieee.org/scv-cas</a></p>
<p>
This is a review of the field of Digital Signal Processing (DSP) and is intended
for those who do not necessarily use DSP on a daily basis.

I am running PhoneGap 3.3 and JQuery 1.4

Edit: page content from my html page where I am adding the above content:

<div data-role="content">
    <div id="page-title"></div>
    <div id="page-region"></div>
    <div id="page-content"></div>
</div> 

Upvotes: 1

Views: 124

Answers (2)

anurag_29
anurag_29

Reputation: 932

You don't need to split. Use $(this).attr('href') instead of this.href.split('=')[1]. Should work just fine!

Updated answer:

Use this instead :

$('#page-content a').click(function(){
    currentPage = $(this).attr('href');
    window.open(currentPage, '_blank', 'location=yes')
});

Actually the problem was that the variable currentPage was in quotes. I have changed window.open('currentPage', '_blank', 'location=yes')to window.open(currentPage, '_blank', 'location=yes').

If you don't want to use jquery then replace $(this).attr('href') by this.href. Here's a working fiddle : http://jsfiddle.net/R89LW/

Upvotes: 1

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

try

 $('#page-content a').click(function () {
            currentPage = $(this).attr("href");
            window.open(currentPage, '_blank', 'location=yes')
        });

Upvotes: 0

Related Questions