Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS binds unsafe:javascript:void(0) when value is javascipt:void(0)

Tags:

html

angularjs

<a ng-attr-href="{{page==1 && 'javascript:void(0)' || '#a/'+tid+'/'+(page-1)}}">Prev</a>

I want to get that when page = 1

<a href="javascript:void(0)">Prev</a>

But the result:

<a href="unsafe:javascript:void(0)">Prev</a>
like image 384
Mahesh Thumar Avatar asked Sep 24 '14 10:09

Mahesh Thumar


People also ask

What does javascript void 0 do?

JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.

Should I use javascript void 0?

Combining javascript: and void(0) Sometimes, you do not want a link to navigate to another page or reload a page. Using javascript: , you can run code that does not change the current page. This, used with void(0) means, do nothing - don't reload, don't navigate, do not run any code.

How do I set javascript to void 0?

Output: Using “javascript:void(0);” in anchor tag: Writing “javascript:void(0);” in anchor tag can prevent the page to reload and JavaScript functions can be called on single or double clicks easily. Example: html.

Which href value should I use for javascript links or javascript Void 0?

The href= “” will only load the current page, while href= “#” scrolls the current page to the top, while href= 'javascript:void(0)' will do nothing at all. The same effects of javascript:void(0) are realized by returning false from the click event handler of the <a> tag with either of the two methods.


2 Answers

use ng-click on a and pass $event to your function.

<a href="#" ng-click="yourFunction($event);">Click</a> 

In your function do the following

$scope.yourFunction = function(e) {
        e.preventDefault();
};
like image 77
Raj Avatar answered Sep 18 '22 15:09

Raj


new angular you have to white list javascript

angular.module("app", [])
    .config(['$compileProvider', function ($compileProvider) {
        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|javascript):/);
    }]);

or

<a href="#" ng-click="foo($event);">Click</a> 
$scope.foo = function(e) {
        e.preventDefault();
};

or

<a ng-click="foo($event);">Click</a> 
like image 33
Sarath Ak Avatar answered Sep 20 '22 15:09

Sarath Ak