Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularjs unsafe ng-href using javascript: void(0); [closed]

Tags:

angularjs

I have a similar question to this post.

But adding "javascript" what was proposed in the answer didn't resolve my problem:

$compileProvider.urlSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/);

My code looks something like:

<a ng-href="{{var1 ? '/home' : 'javascript: void(0);'}}" ng-bind="var2"></a>

But in the url, it produces an unsafe:javascript: void(0);

like image 566
john smith Avatar asked Oct 01 '13 23:10

john smith


People also ask

Should you use JavaScript void 0?

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. The "Link" word is treated as a link by the browser. For example, it's focusable, but it doesn't navigate to a new page.

What is JavaScript void 0 in href?

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.


1 Answers

I assume you use the latest version of AngularJS. The method name has been changed since that SO answer.

myapp.config(function($compileProvider){
  $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|file|javascript):/);
});

Demo link.

like image 134
Buu Nguyen Avatar answered Sep 22 '22 16:09

Buu Nguyen