Kai Hirst
Kai Hirst

Reputation: 11

Including a Razor formatted String in a Javascript Function

@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

Upvotes: 0

Views: 1820

Answers (2)

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14292

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.

Upvotes: 2

Peter Smith
Peter Smith

Reputation: 5550

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

Upvotes: 0

Related Questions