Forum Moderators: travelin cat
This code in our .NET site doesn't allow Safari users (or any mac browsers) to open the following pages as it does for Windows IE users. Help appreciated:
string strHREF="javascript:window.open('CurrentConference.aspx?CurrentConfID="+ dt.Rows[index][0].ToString(); strHREF += "&ConfName=" + dt.Rows[index][1].ToString().Replace("'", "") +"','_self')";
dt0 = 'something';
dt1 = "'something'";
strHREF="javascript:window.open('CurrentConference.aspx?CurrentConfID="+ dt0.toString();
strHREF += "&ConfName=" + dt1.toString().replace("'", "") +"','_self')";
alert(strHREF);
I removed the "string" from "string strHREF." There is no chance that javascript is going to type that declaration as an integer, so you can safely remove it. If you were worried, you could just predeclare it as an empty string like so:
strHREF = '';
I also fixed the case of ".toString" and ".replace" and, using my fake variables, it now works in Safari and Firefox. Now, your data structure may also be a source of problems, but I'd need a little more info to debug those. (Although I could guess that the [index] one is problematic considering that Javascript arrays are recommended to be numeric.)
Two things breaking it from what I can tell... The attempt at string typing and some unNecessary camelCasing. ;) Now, I don't have your data structure (the dt.whatever) so I had to remove that bit to get my sample to work, but here's working code:
dt0 = 'something';
dt1 = "'something'";
strHREF="javascript:window.open('CurrentConference.aspx?CurrentConfID="+ dt0.toString();
strHREF += "&ConfName=" + dt1.toString().replace("'", "") +"','_self')";
alert(strHREF);
I removed the "string" from "string strHREF."
[OVRDRVN] You cannot remove string, otherise this code will not compile.
There is no chance that javascript is going to type that declaration as an integer, so you can safely remove it. If you were worried, you could just predeclare it as an empty string like so:
strHREF = '';
I also fixed the case of ".toString" and ".replace" and, using my fake variables, it now works in Safari and Firefox. Now, your data structure may also be a source of problems, but I'd need a little more info to debug those. (Although I could guess that the [index] one is problematic considering that Javascript arrays are recommended to be numeric.)
[OVRDRVN] - I don't think you need to do this
This is not a problem.
The only problem i see is the way i am trying to open the window. How is href -(address) placed in the mac browsers we want to use? It seems that they are sending the data back to the server via "onclick" but not redirecting to the new page (the formula after the actual page (currentconference.Aspx) where windows users get?Confid=36&confname=XXXX and thus go to the right place. Testing in Firefox, I can manually put in the url after clicking the appropriate conference and get to the right page...this trick doesn't work in Safari.
How is href -(address) placed in the mac browsers we want to use?
Ahhh, I understand, the problem is not that your code is failing to generate the correct code, but how that code is being interpreted by the browser? I thought your posted code was the actual on-page javascript.
What does the actual code served to the page look like for your link that contains the "javascript:window.open" bit?
<script type="text/JavaScript">
<!--
function MM_openBrWindow(theURL) { //v2.0
window.open(theURL,'_self');
}
//-->
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form name="Form1" method="post" action="CurrentConference.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwxNzcyNjM0OTY0Ozs+9NzPBLBYyfcBEhh5d0cxBMjJaCQ=" />
<TABLE width="100%" align="center">
<TR>
<TD align="left"><IMG alt="" src="images/CajeLogo.jpg"></TD>
<TD class="headerStyle" align="left">Session and Presenter Management</TD>
</TR>
</TABLE>
<BR>
<BR>
<DIV class="PageHeaderStyle" align="center">Choose a Conference to work on:</DIV>
<DIV align="center"><BR>
<BR>
<div id="buttonPanels">
<button id="btn36" Class="ButtonStyle" OnClick="MM_openBrWindow('CurrentConference.aspx?CurrentConfID=36&ConfName=CAJE 31')">CAJE 31</button><br><br><button id="btn37" Class="ButtonStyle" OnClick="MM_openBrWindow('CurrentConference.aspx?CurrentConfID=37&ConfName=Early Childhood and Dayschool Conference')">Early Childhood and Dayschool Conference</button><br><br>
</div></DIV>
<BR>
<BR>
<BR>
<DIV class="PageHeaderStyle" align="center">View/Add CAJE Conference</DIV>
<BR>
<DIV align="center"><A href="ViewConferences.aspx">View/Add Conferences</A></DIV>
</form>
</body>
</HTML>
The
<button>elements are behaving like a
<submit>when it is clicked and the
form action="CurrentConference.aspx"is "firing" before the
onclickhas a chance. Make sense?
I bet there's at least a handful of different ways to address this issue. Realizing that your current script already leaves javascript disabled users in the cold, here's a quick and dirty way to get it working:
Change the action of the form itself using javascript
Your function becomes:
function MM_openBrWindow(theURL) { //v2.0
document.getElementById('Form1').action = theURL;
document.getElementById('Form1').onsubmit();
}
Above code tested and working as expected in Safari and Firefox on Mac. I can provide version numbers if you like. :)
Added: Let me know if this works in IE, I'm a bit too busy on other work to do an exhaustive test right now.
CurrentConference.aspx?CurrentConfID=36&ConfName=CAJE 31 is not a valid URL. URLs are not allowed to have whitespace. Perhaps MSIE/.NET is 'helping' you out by interpreting the whitespaces as
%20, which is how they should be written in your Javascript code, too. Use a nested replace() function to fix the whitespace ... which leads to ... It really DOES make a difference how you capitalize Javascript functions. For example, it really IS
replace(), not Replace(). Just because it's working for a Microsoft browser in your Microsoft .NET framework doesn't means the syntax is universally valid. More likely it means that the MS products are 'helping' you by 'fixing' the code. However that won't help with a client-side solution like Javascript which relies on the client to do the interpretation. Did you install the .NET framework on the Mac client? If not, and if it's working on a .NET-capable Windows machine (SP2), you may want to consider guaranteeing that your code follows the true specification to the letter, rather than on what the MS product combination seems to be able to make work for you. Lastly, I've had problems in the past like this where the URI being sent to a Javascript
window.open function needed to be compiled and stored as a variable before it was sent to the function. i.e. loc1="yadda.php?yadda1=1&yadda2=2"; and then onclick="doOpen(loc1)". It seemed like the act of parsing the entities and preparing to send the URI via the JS engine was funky on the more strict browsers, but I never figured it out beyond the fix shown above. (A quick note, whoisgregg: The form BUTTON element does not act as a submit button ... it's just a widget. Clicking on the button should not submit the form ... in fact, it's curious how the form's HIDDEN element will be used, as there is no mechanism in the posted code for submitting the form at all. Is it possible that .NET apps like this DO use the BUTTON element for submission? That would be wacky and confusing.)
The form BUTTON element does not act as a submit button ... it's just a widget.
Yeah, I actually knew that. Yet, when I test the code from msg #5 in Safari and Firefox and click either button, the browser will POST the form and the hidden field is available on the next page. I found it quite surprising myself.
(I make only a single change to the provided code, I point the form action to my own "CurrentConference.php" page that print_r's the GET and the POST arrays.)
Added:
Is it possible that .NET apps like this DO use the BUTTON element
Just to clarify, I ran the test page on Apache and had the behavior I described above.
I tried the rendered code (straight HTML and JS copied from above) from my desktop in a simple test and got this response:
1) Click either button
2) Page not found (of course): Just the page, no query
3) Back button: Page not found: Page PLUS query!
4) Back button, again: Back to the simple test page
So it seems to have (a) requested the page+query then (b) immediately jumped to page-query. So quick I couldn't detect the first try. (FF1.5x+Win98SE ... figured it couldn't hurt to test it, non-Mac. ;) )
Same whether I replace the whitespace with entities or not, so that notion goes out the window, I guess.
I don't know what to make of it ... just reporting.