4 Replies - 2305 Views - Last Post: 08 July 2015 - 01:36 PM Rate Topic: -----

#1 depricated  Icon User is online

  • Nero


Reputation: 2306
  • View blog
  • Posts: 5,968
  • Joined: 13-September 08

JavaScript in Code Behind

Posted 07 July 2015 - 01:44 PM

So I've been fighting with an issue for a while. In the process I've found this atrocity hidden in our base page.

        Dim sb As StringBuilder = New StringBuilder()
        sb.Append("function CloseSessionInstance(closeOnCallback){")
        sb.Append("var otherData = redactedPath.Sessionhelper(); otherData.ClientY = window.event.clientY; otherData.ClientX = window.event.clientX; otherData.ClientWidth = document.documentElement.offsetWidth;")
        sb.Append("var instanceId = ""0"";")
        sb.Append("var pos1 = window.location.search.search(/IID/);")
        sb.Append("if (pos1 != -1) instanceId = window.location.search.substr( pos1 + 4, 1 );")
        'sb.Append("if ( closeOnCallback ) { PageMethods.RemoveSessionInstance(instanceId,OnRemoveSessionInstanceSucceeded); }")
        sb.Append("if ( closeOnCallback ) { PageMethods.RemoveSessionInstance(instanceId,otherData,OnRemoveSessionInstanceSucceeded); }")
        'sb.Append("else { PageMethods.RemoveSessionInstance(instanceId); } }")
        sb.Append("else { PageMethods.RemoveSessionInstance(instanceId, otherData); } }")
        sb.Append("function OnRemoveSessionInstanceSucceeded(result){ window.close(); SessionCleared = true; }")
        ClientScript.RegisterStartupScript(Me.GetType(), "CloseSessionInstance", sb.ToString(), True)


This made me cringe so hard, especially since I'm right in the middle of helping write our JS standards, which have clearly never been defined.

This function is called by a button that appear on almost every page - a "Close Session" button. That is the only thing that ever calls it, though. It is never referenced anywhere else, ever. The button always passes the parameter of True for "closeOnCallback" - that parameter doesn't need to exist. Since it's always true, it never needs to be checked. There's nothing that changes that param to false, ever.

Better yet, there's no PageMethods.RemoveSessionInstance() that matches the signature use in the case that it IS true. The definition of the function accepts 2 parameters. So it errors out the js on that line and aborts. For some reason it keeps going for me, but seems to break on any user machine. That part's really thrown me.

So for testing and really to make it much cleaner, I'm nuking the above code and I've moved it to a .js file that's already being registered on the page. There's no reason I ca conceive of for it to be done that way.

function CloseSessionInstance() {
    var otherData = new redactedPath.Sessionhelper();
    otherData.ClientY = window.event.clientY;
    otherData.ClientX = window.event.clientX;
    otherData.ClientWidth = document.documentElement.offsetWidth;
    var instanceId = window.getInstanceId();
    PageMethods.RemoveSessionInstance(instanceId, otherData);
    window.close();
    SessionCleared = true;
}


Thought I'd share this thing that's been plaguing me. Bleh.

Is This A Good Question/Topic? 0
  • +

Replies To: JavaScript in Code Behind

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: JavaScript in Code Behind

Posted 07 July 2015 - 03:48 PM

"Holy diabolical JS in a button Batman!" - Quote from comic #48 of "Martyr2 vs the Terrible Newbie JS Gang"

This post has been edited by Martyr2: 07 July 2015 - 03:49 PM

Was This Post Helpful? 3
  • +
  • -

#3 Gorian  Icon User is offline

  • ninja DIC
  • member icon


Reputation: 153
  • View blog
  • Posts: 1,853
  • Joined: 28-June 08

Re: JavaScript in Code Behind

Posted 08 July 2015 - 08:19 AM

man, where can I get that comic at? Sounds pretty awesome.
Was This Post Helpful? 2
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: JavaScript in Code Behind

Posted 08 July 2015 - 12:11 PM

Yeah it is pretty rare. I managed to get a copy when I was at the last San Diego comic con. You might be able to find one on ebay or something. I did take a screenshot of it for you... great comic!

Attached Image


:D
Was This Post Helpful? 3
  • +
  • -

#5 depricated  Icon User is online

  • Nero


Reputation: 2306
  • View blog
  • Posts: 5,968
  • Joined: 13-September 08

Re: JavaScript in Code Behind

Posted 08 July 2015 - 01:36 PM

haha that's awesome

And today I found this

        If (RemoveSessionOnClose And ConfigHelper.HandleBrowserClose()) Then
            ' Generate client side script for handling browser window close (via close button on window or tab only)
            sb.Remove(0, sb.Length)
            sb.Append("window.onunload=function(){")
            sb.Append("var isIE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1);")
            sb.Append("if (isIE6) {")
            sb.Append("if (window.event.clientY < -1000 && window.event.clientX < -1000) { CloseSessionInstance(false); }")
            sb.Append("} else {")
            sb.Append("var closeOffset = 5;")
            sb.Append("var tabOffset = -20;")
            sb.Append("var widthOffset = 20;")
            sb.Append("var isIE8 = (navigator.userAgent.toLowerCase().indexOf('msie 8') != -1);")
            sb.Append("if (isIE8) { closeOffset = 40; tabOffset = -26; widthOffset = 0; }")
            sb.Append("if ((window.event.clientY < 0) && (window.event.clientX - widthOffset <= document.documentElement.offsetWidth) && (window.event.clientX > (document.documentElement.offsetWidth - closeOffset))")
            sb.Append("|| ((window.event.clientY < 0) && (window.event.clientY >= tabOffset) && (window.event.clientX > 0))) {")
            sb.Append("CloseSessionInstance(false); }}}")
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "HandleWindowClose", sb.ToString(), True)
        End If


I can't even decipher what it's trying to determine with that if statement at the end
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1