c# - Modifying Javascript Variables Within A WebBrowser Control -


i have webpage when visited, declares variable called date using following:

var date=new date("03 oct 2013 16:04:19"); 

that date displayed @ top of page. there way me modify date variable? (and not visible html source)

i have been experimenting invokescript finding hard grasp, if knows , post examples relating directly i'd hugely grateful. thankyou.

you can inject javascript code using javascript's eval, works ie version. you'd need make sure page has @ least 1 <script> tag, easy:

public class form1      private sub form1_load(sender object, e eventargs) handles mybase.load         call webbrowser1.navigate("http://example.com")     end sub      private sub webbrowser1_documentcompleted(sender object, e webbrowserdocumentcompletedeventargs) handles webbrowser1.documentcompleted         '         ' use webbrowser1.document.invokescript inject script         '         ' make sure page has @ least 1 script element, eval works         webbrowser1.document.body.appendchild(webbrowser1.document.createelement("script"))         webbrowser1.document.invokescript("eval", new [object]() {"(function() { window.newdate=new date('03 oct 2013 16:04:19'); })()"})         dim result string = webbrowser1.document.invokescript("eval", new [object]() {"(function() { return window.newdate.tostring(); })()"})         messagebox.show(result)      end sub  end class 

alternatively, can use vb.net late binding call eval directly, instead of document.invokescript, might easier code , read:

public class form1      private sub form1_load(sender object, e eventargs) handles mybase.load         call webbrowser1.navigate("http://example.com")     end sub      private sub webbrowser1_documentcompleted(sender object, e webbrowserdocumentcompletedeventargs) handles webbrowser1.documentcompleted          '         ' use vb late binding call eval directly (seamlessly provided by.net dlr)         '         dim htmldocument = webbrowser1.document.domdocument         dim htmlwindow = htmldocument.parentwindow         ' make sure page has @ least 1 script element, eval works         htmldocument.body.appendchild(htmldocument.createelement("script"))         htmlwindow.eval("var anotherdate = new date('04 oct 2013 16:04:19').tostring()")         messagebox.show(htmlwindow.anotherdate)         ' above shows don't have use javascript anonymous function,         ' it's coding style so, scope context:         htmlwindow.eval("window.createnewdate = function(){ return new date().tostring(); }")         messagebox.show(htmlwindow.eval("window.createnewdate()"))          ' can mix late binding , invokescript         messagebox.show(webbrowser1.document.invokescript("createnewdate"))      end sub  end class 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -