1

When I tried to put a simple javascript inside the Insert Javascript of General Link it wasn't able to make it properly work as I noticed it appends a return false which causes an error.

Simple Javascript Alert

The rendered markup is below.

<a href="javascript:onclick=alert("test");return false;" >Test</a>

Kindly help me on how I can resolve this or if there is a setting I need to do to make it work by default. Thanks.

0
0

Try to take a anchor with link "javascript:void(0)" and use the onclick field.

This will add a hashtag infront of "javascript:void(0)", which means that it ends up looking like this:

 <a href="#javascript:void(0); onclick="javascript:alert('test');">Test</a>
1

You cannot (easily) change this. The way to get a URL string from a General Link field is via the GetFriendlyUrl() method. It looks like this.

public string GetFriendlyUrl()
{
  return new LinkUrl().GetUrl(new XmlField(this.InnerField, string.Empty), this.InnerField.Database);
}

Inside the LinkUrl class, we find the GetJavaScriptUrl method, and as you can see - the behaviour with forcing return false; into the script is hard coded.

protected virtual string GetJavaScriptUrl(string url)
{
  Assert.ArgumentNotNull((object) url, nameof (url));
  if (!url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase))
    url = "javascript:" + url;
  return url + ";return false;";
}

So to do it, you would need to bypass this behaviour by making your own LinkUrl class and overriding this method.

public class MyLinkUrl : LinkUrl
{
    public override virtual string GetJavaScriptUrl(string url)
    {
        string b = base.GetJavaScriptUrl(url);
        return b.Replace(";return false;", ";");
    }
}

And then generate your link without using the method directly on LinkField;

return new MyLinkUrl().GetUrl(new XmlField(this.InnerField, string.Empty), this.InnerField.Database);

(code written directly in editor here, might have small syntax mishaps)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.