0
@foreach (var item in Model.policydata)
{

<tr align="center">
<td>
@Html.DisplayFor(modelitem => item.Name)
</td>
<td>
@Html.DisplayFor(modelitem => item.PolicyID)
</td>
<td>
@Html.DisplayFor(modelitem => item.CustomerID)
</td>
<td>            
<a onclick="OpenRepositoryFile()" href="@string.Format("http://repository.website.com/{0}/{1}.pdf", Model.PolicyName, item.CustomerID)">View</a>       
</td>
</tr>
}


</table>
<script>

    function OpenRepositoryFile()
    {

        var win = window.open('', '_blank');
        if(win) 
        {
            //Browser has allowed it to be opened
            win.focus();
        }else{
            //Broswer has blocked it
            alert('This application feature requires Popups Enabled. Please right click to open in a new Tab or change your Browser settings');
        }
    }
</script>

Ive got this Razor code and this Javascript function. Basically, the Razor link is built from data retrieved from model variables, and the Javascript function opens a new window/tab (settings dependant) or, alerts the user if browser settings do not allow popups. When used separately, both work fine.

Heres the problem. I want the Razor built link as an argument in the window.open command of the Javascript function.

I would've thought that in Javascript or JQuery, there would be a string.format() function as there is in .Net by now but, there isn't..

Is there a way of getting the Href string attribute that Razor builds within the Anchor tag by its ID? or any other suggestions on this?

Thanks

2 Answers 2

2

You could do this very easily with jQuery, only you need to bind an event with this anchor tag via jQuery just like below:

HTML

<a id="ancWindow" 
    href="@string.Format("http://repository.website.com/{0}/{1}.pdf", Model.PolicyName, item.CustomerID)">View</a>  

jQuery

$(document).ready(function() {
    $('#ancWindow').click(function(e) {
        e.preventDefault();  // <- used to prevent redirection from anchor
        var win = window.open(this.href, '_blank');
        if(win) {
            //Browser has allowed it to be opened
            win.focus();
        } else{
            //Broswer has blocked it
            alert('This application feature requires Popups Enabled. Please right click to open in a new Tab or change your Browser settings');
        }
    });
});

Note: Make sure you have the reference of jQuery file.

Sign up to request clarification or add additional context in comments.

1 Comment

Brilliant bit of code. Was looking at Jquery but, my Code knowledge is lacking in this department and does need improving... There is an issue however. The function does work and opens but the Alert pops up even though the tab opened. This to do with browser settings? or can further work be done to the code? Thanks very much so far through...
0

As I understand it the href and the onclick together are incompatible as they both do the same thing.

To create the parameter think of it as a string

@{
    String tempString = string.Format("http://repository.website.com/{0}/{1}.pdf", Model.PolicyName, item.CustomerID)"
}

Then your call becomes:

OpenRepositoryFile(@(tempString))

Warning - not tested

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.