views:

140

answers:

2

Extending a website I didn't build. I want to be able to call the ShowWindow proc with a parameter. How would I do that? New to JQuery and Javascript.

default.aspx

<script type="text/javascript" src="/scripts/jquery-1.2.6.js"></script>
<script type="text/javascript">

$(document).ready(function() {
    $('a#ShowWindow').click(function() {
        window.open("TerminalInfo.aspx?", 'Info', 'scrollbars=yes,width=510,height=536');
    })
});

default.aspx.cs

Building the aspx dynamically...

    public static string ToHtml(this Location location)
    {
        var html = new StringBuilder();

        html.Append("<td><a href='#' id='ShowWindow'>ShowLetter</a></td>");     //This works
        html.Append("<td><a href='#' id='ShowWindow(\"MyInfo.aspx\")'>Read More</a></td>"); //How can I do this? It doesn't work.

        return html.ToString();
    }
A: 
var strattonn = {};
strattonn.openWindow = function(url, title) {
    window.open(url, title, 'scrollbars=yes,width=510,height=536');
    return false;
}


public static string ToHtml(this Location location)
{
    var html = new StringBuilder();
    html.Append("<td><a href='#' onclick='return strattonn.openWindow('MyInfo.aspx', 'My Info')'>Read More</a></td>");
    return html.ToString();
}
Shawn Simon
ok so i know the onclick attribute is a bad practice;p
Shawn Simon
+6  A: 
public static string ToHtml(this Location location)
{
    var html = new StringBuilder();

    html.Append("<td><a href='MyInfo.aspx' id='ShowWindow'>Read More</a></td>");

    return html.ToString();
}

and then

$('a#ShowWindow').click(function(e) {
    window.open($(this).attr("href"), 'Info', 'scrollbars=yes,width=510,height=536');
    e.preventDefault();
})

It's a little different approach but it degrades better if JavaScript is unavailable.

Update (to work on several links in table)

$('table a').click(function(e) {
    window.open($(e.target).attr("href"), 'Info', 'scrollbars=yes,width=510,height=536');
    e.preventDefault();
});
Daniel
+1 And is the only solution that doesn't make use of the onclick attribute. I was beginning to lose faith.
Justin Johnson
Yes, better than mine. Mine was simply responding to the obvious error. I guess if he is going to learn JavaScript on this project we should help him learn correctly.
sheats
the only problem i see is that you aren't returning false, so the page will change
Shawn Simon
added prevent default.
Daniel
I appreciate the "correct" help! Let me work this over today and see if I can make it sing.
strattonn
Thanks all. Got that to work but one question/problem. I create the html dynamically because there is a table of links. So rather than just MyInfo.aspx I call MyInfo.aspx?LocationId=x. The first one works great but for the rest it looks like the javascript is not called and the page is not loaded in its own window.Any ideas?
strattonn
see updated answer...
Daniel