locked
How to undo an href link in razor code RRS feed

  • Question

  • User1868852945 posted

    I have an href with an onclick window.open which opens the next page:

    <a href='#' onclick='window.open(connect_url("@item.object_name","@item.capture_date","@item.capture_id"), "_blank")'>@item.object_name</a>

    I want to add logic that compares two variables where the next page will only open when they are equal.  If they are not equal I do not want the open to occur (nothing will happen).

    I tried adding the compare logic in bold font below which does not compile.  Can someone please advise on how to do this?  How can I effectively cancel the href? Thanks.

    <a href='#' onclick='window.open(if (var1 == var2) {connect_url("@item.object_name","@item.capture_date","@item.capture_id")}, "_blank")'>@item.object_name</a>

    Saturday, November 19, 2016 3:44 PM

Answers

  • User2103319870 posted

    I want to add logic that compares two variables where the next page will only open when they are equal.  If they are not equal I do not want the open to occur (nothing will happen).

    I think you are using Razor view, if that is the case then you can try with the below code

    @if (var1 == var2)
    {
        <a href='#' onclick='window.open(connect_url("@item.object_name","@item.capture_date","@item.capture_id"), "_blank")'>@item.object_name</a>
    }
    else
    {
        <a>@item.object_name</a>
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 19, 2016 4:21 PM