How to prevent < a> tag redirect action in Ionic?


#1

I want to use cordova’s inappbrowser to open the url of < a> tag. But when I click the < a>, it will open the url in a new page inside my app. I tried to prevent the redirect using e.preventDefault() and return false but failed. So is there any way to prevent the redirect action of < a> tag?
e.preventDefault() works well when I using jquery mobile. Is ionic rewrite the preventDefault() or it’s because of the “touch” gesture?


#2

I’m pretty new here too - but I use ng-href instead of href, and it just works. It also lets you interpolate variables like {{item_id}}.

Have you tried that?


#3

I tried ng-href, but it dosen’t work either. What I want to do is like this:

<a onclick="openHref(this)" href="http://www.google.com">google</a>

<script>     
   var newRef;
   function .openUrl(e){
       e.preventDefault();     //it dosen't work, the href will redirect anyway
       var elemUrl = this;
       var temp = $(elemUrl).attr("href");

      //open the href by cordova inappbrowser 
       newRef = window.open(temp, '_system', 'location=no');  
    };
I want to stop the default redirect action of < a>, the code works well in a jquery mobile app.

#4

You could try making sure teh function returns false. Or return false explicitly in the onClick.

I haven’t (yet) been able to grasp how to use my regular (jquery/etc) javascript experience along with angular. The tutorials I’ve read have said to try doing things “the angular way” for a few weeks first.

What happened when you tried ng-href? You shouldn’t need to be messing with url-redirection stuff…

Kevin