Search code examples
javascriptinline

Java Script inline function for Anchor tag


I'm trying to open a URL in new window and want to share the current page address using inline function as below:

a href="javascript:void(0);" onClick='(function()
{
    var link = string.concat("example.com/UserStatus.phpid=99244613&utm_source=",window.location.href);
    console.log(link);
});return false;'>click here</a>

But nothing is happening, please help.


Solution

  • The problem was that you were using string.concat but the right way to use is "".concat:

    function fun(){
      var link = "".concat("example.com/UserStatus.phpid=99244613&utm_source=", window.location.href); 
      console.log(link); 
    }
    
    fun();
    

    Output will be:

    example.com/UserStatus.phpid=99244613&utm_source=https://playcode.io/

    HTML:

    <a href="javascript:void(0);" onClick='fun();'>click here
    </a>
    

    So in your code just use "".concat instead of string.concat.