locked
OnChange events in Javascript via PowerShell RRS feed

  • Question

  • PowerShell question - Does anyone know how to fire the onchange event in Javascript? I navigate to a page and select an option from a drop-down menu. I can change the value in the drop-down list but the DOM object doesn’t register the onchange event. There’s a Technet article that explains to use a Silverlight .NET function to accomplish the onchange event – but is this right?

    Link: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/d051aad4-58d6-4dd0-836d-a3ba0b170e80/

    Reason I question is, I doubt the page I’m working on is built on Silverlight so could a Silverlight .NET function really trigger my onchange event?

    At anyrate, if anyone knows how to get PowerShell to fire the onchange event in javascript?

    Here’s the onchange code: <select name="dtlSOMNew$ddCustA" id="dtlSOMNew_ddCustA" style="font-size: smaller;" onchange="javascript:setTimeout('__doPostBack(\'dtlSOMNew$ddCustA\',\'\')', 0)">

    I can change the value in the drop down but need to execute the change by triggering the onchange event. Any help would be so appreciated!

    • Edited by LarryHu Wednesday, December 7, 2011 6:06 AM
    • Moved by Rich Prescott Thursday, December 15, 2011 10:21 PM Interoperability (From:The Official Scripting Guys Forum!)
    Wednesday, December 7, 2011 6:05 AM

Answers

  • Sorry - I don 't want to get into a long discussion.

    The simple issue is that you cannot do what you are trying to do.  Web pages a run and secured in a way that is designed to prevent exactly what you are trying to do. The sandboxing of the page may allow some things to work partially.

    Please study web developement a bit more and study the HTML DOM.  All examples of eventing on the DOM are done from script within the webbrowser and not from an external program.

    There are ways to repurpose a web page using the developer and debug APIs which are not, to my best knowledge, available from within PowerShell by any normal means.

    Your best bet is to try and do direct forms submission. You need to discover the form and all of its values and create a request that will post the form to the server directly with the caviat that it is not a FORMS based authentication system.

    The technet solution  was a suggested solution that "might" work.  No indication was given that it was ever tested.  I say it won't work.

     jv

    Wednesday, December 7, 2011 8:26 PM

All replies

  • You are asking NET questions. .NET and SilverLight are not admin scripting.  You need to go to a web developer forum to ask this kind of question.

    I would start by getting a book or tutorial on .NET coding and ASP.NET with Silverlight.  Your question shows you do not understand the basics of web programming.

     

     


    jv
    Wednesday, December 7, 2011 6:12 AM
  • You are asking NET questions. .NET and SilverLight are not admin scripting.  You need to go to a web developer forum to ask this kind of question.

    I would start by getting a book or tutorial on .NET coding and ASP.NET with Silverlight.  Your question shows you do not understand the basics of web programming.

     

     


    jv


    What a negative, abusive post.  Well, IMO, your assessment of my web programming skill level doesn't help a whole lot...  I suppose if you wanted to play the role of an authoritarian teacher in a strict classroom perhaps it makes sense.  I never claimed to be an expert web programmer and in fact came here because I've come to expect that TechNet forums are a safe and positive place to learn!   I do not like how you worded out your second sentence because it's inferring that you're making me out to be some sort of fraud - to which I never claimed any proficiency in web programming.  FYI, I've posted this same question on various PowerShell/scripting forums, (Experts-Exchange and Powershellcommunity.com) to no avail so it's not a gratuitously basic question.  For someone with as many posts as you have, I would have thought your "help" would have been presented a little more positively!  I don't think I deserved what you dished out.  The way you speak sounds like you have some personal vendetta against me or maybe you just had a bad day.

    For what it is worth, I've scoured the internet and there is little to nothing on triggering a JavaScript onchange event through PowerShell.  There is a lot of help available on how to do it through Javascript itself, but not through PSH. 

    Additionally, the link I provided references Silverlight code which my code is not Silverlight based at all.  So my question was to try to get some more detail.  Such as, will non-Silverlight code work against a Silverlight .NET function? 


    The reason I even p osted on here was because I saw people posting about Javascript/Powershell questions on this forum, such as

    here

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/fb458d88-131e-4c93-931a-4e9a47676082

    or here

    http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/cf5a54c0-a627-4517-96f8-79d3b89fc533. 

    Just didn't expect to get so much flak over it!  What a scolding!  To each his own I guess.









    Wednesday, December 7, 2011 8:33 AM
  • I don't think you understand what is happening with events.  Javascript does not have events. The object has events which can call into a script or into code.

    Thisis why I suggested you get the basics of programming first.  If yu do not understand teh system model or teh eventing model of an object and how events are dispatched you will ask questions that have no answer.

    PowerSHell can look at objects. At times and under some circumstances POwerShell can recieve evnts and can send events to an object.

    SilverLight uses an object model that is not part of the normal PowerShell system.  If your web page is using JavaScript on the client then I suspect it is running in a browser.  It is not normally possible to access a web page running in a browser although you can access the DOM of the page.  YOu may even be able to trigger some of the events on the browser.

    You cannot cause events in JavaScript as JavaScript has no events.

    The article you posted sggests a method for calling a page event in SilverLight.  It never indicates if that worked.  I say it won't work.  If you had tried it you would see that it doesn't work.

    The code you posted is NOT onchange code.  It is a slelct tag with embedded JavaScript that gets executed when the select value changes via the user interface or via an assignment. 

    You can try that.  It will work on a stait page that you build from scratch in POwerShell but will likely not work on a page loaded from a web server.

    I will not work at all on a SilverLight page.

    Again - if you want to better understand NET programming I highly recommend starting with a book on the subject.

    You also state that you can change to select list value but really want to do it with the onchange event.  TRy to understand that an event is a response to an action.  Changing the list will trigger the event and not the other way around.  Triggering the event in your example would cause a delayed postback. 

    The only way to force an event is to call it <object>.onchange() which will not normally work on a page loaded from a web.

    I still don't understand what any of this has to do with SilverLight other than it was an example that didn't work.

    I suppose an easier way to try an approach this is to ask why you would want to do this? If you want to submit a form thenyou only need to build the form variables as an array and post them to the server.  Ther eis no need to force the page to do it for you.  You need to first be able to authenticate on the server.  Some servers will never allow programitic post-back to prevent hackers frm accessing the server.  These servers will likely not allow logon except thtough a session managed forms-based logon page.

     

     


    jv
    Wednesday, December 7, 2011 9:28 AM
  • I don't think you understand what is happening with events.  Javascript does not have events. The object has events which can call into a script or into code.

    That I do not understand what's happening with events is partially true.  My terminology is pretty loosely used but I know there are events that I want to trigger.  This is not my usual domain so you'll have to bare with me. 

    Thisis why I suggested you get the basics of programming first.  If yu do not understand teh system model or teh eventing model of an object and how events are dispatched you will ask questions that have no answer.

    While it isn't programming that I am new to, it's this particular scenario with using PowerShell to manipulate a web page which has Javascript. For the most part I am able to automate what I need thus far (one example was loading a page and pulling 3 columns of data from the Javascript into 3 PSObjects to pump those objects into another script to essentially cut down on a lot of copy/paste).

    PowerSHell can look at objects. At times and under some circumstances POwerShell can recieve evnts and can send events to an object.

    For me, I am not focusing too much on the terminology, one part a function of newness to Javascript and another part just wanting to focus on the components that I'm getting stuck on and hoping someone else can interpret where I'm at if I'm not being 100% accurate with my scenario.

    SilverLight uses an object model that is not part of the normal PowerShell system.  If your web page is using JavaScript on the client then I suspect it is running in a browser.  It is not normally possible to access a web page running in a browser although you can access the DOM of the page.  YOu may even be able to trigger some of the events on the browser.

    Yeah, I figured SilverLight isn't part of the normal PSH system.  I looked into loading the assemblies for the System.Windows.Browser.HTMLElement Silverlight class and quickly found out it's not part of the usual .NET binaries.  I had trouble loading the Silverlight assembly even after following just about every guide I could find before testing my patience and came to the thought - "Will this Silverlight function even work on a non-Silverlight code?" My thoughts centered on 'no'.

    You cannot cause events in JavaScript as JavaScript has no events.

    The article you posted sggests a method for calling a page event in SilverLight.  It never indicates if that worked.  I say it won't work.  If you had tried it you would see that it doesn't work.

    The code you posted is NOT onchange code.  It is a slelct tag with embedded JavaScript that gets executed when the select value changes via the user interface or via an assignment. 

    Is there someway to - bare with me where again - instantiate/call/trigger/etc the embedded Javascript?  I am able to get the webpage UI to select the value I need.  It just doesn't execute the code onchange="javascript:setTimeout('__doPostBack(\'dtlSOMNew$ddCustA\',\'\')', 0)" which is the crux of what I really really need. 

    You can try that.  It will work on a stait page that you build from scratch in POwerShell but will likely not work on a page loaded from a web server.

    I will not work at all on a SilverLight page.

    Again - if you want to better understand NET programming I highly recommend starting with a book on the subject.

    I'm definitely not opposed to that.  It's something I'm branching to but hasn't been my usual domain, although I have had a background in Java.  Anyway, for the time being I am hoping to find the bits and pieces I need to finish this solution.

    You also state that you can change to select list value but really want to do it with the onchange event.  TRy to understand that an event is a response to an action.  Changing the list will trigger the event and not the other way around.  Triggering the event in your example would cause a delayed postback. 

    The only way to force an event is to call it <object>.onchange() which will not normally work on a page loaded from a web.

    I tried this before posting this question and there was no onchange() method associated with the select element object, sadly.

    I still don't understand what any of this has to do with SilverLight other than it was an example that didn't work.

    It doesn't really have anything to do with Silverlight.  I was searching for solutions on the web and hit that TechNet link which had been tagged as the accepted solution.  I had my doubts it'd even work, still, but I was determined to ask some people to confirm if it'd work on non-silverlight code as the OP in that link had what appeared to be non-silverlight code as well.

    I suppose an easier way to try an approach this is to ask why you would want to do this? If you want to submit a form thenyou only need to build the form variables as an array and post them to the server.  Ther eis no need to force the page to do it for you.  You need to first be able to authenticate on the server.  Some servers will never allow programitic post-back to prevent hackers frm accessing the server.  These servers will likely not allow logon except thtough a session managed forms-based logon page.

     The reason I'm doing this is to automate the clicking and selecting of drop-down options on a webpage to cut down on a *insane* amount of copy/paste time.  One of the snags I have so far is the webpage isn't updating to reflect a new field once I've selected the drop-down menu option, which is why I am trying to figure out to do "trigger" it.  As for building the form variables as an array and posting it to the server, would you have a quick run-down or link somewhere on it?

     


    jv

    Wednesday, December 7, 2011 8:07 PM
  • Sorry - I don 't want to get into a long discussion.

    The simple issue is that you cannot do what you are trying to do.  Web pages a run and secured in a way that is designed to prevent exactly what you are trying to do. The sandboxing of the page may allow some things to work partially.

    Please study web developement a bit more and study the HTML DOM.  All examples of eventing on the DOM are done from script within the webbrowser and not from an external program.

    There are ways to repurpose a web page using the developer and debug APIs which are not, to my best knowledge, available from within PowerShell by any normal means.

    Your best bet is to try and do direct forms submission. You need to discover the form and all of its values and create a request that will post the form to the server directly with the caviat that it is not a FORMS based authentication system.

    The technet solution  was a suggested solution that "might" work.  No indication was given that it was ever tested.  I say it won't work.

     jv

    Wednesday, December 7, 2011 8:26 PM