Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    In fact guys i'm not putting by that method ... i'm injecting the code with C# like this :

    Code:
      public void OnDocumentComplete(object pDisp, ref object URL)
            {
                HTMLDocument document = (HTMLDocument)webBrowser.Document;
                IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
                    document.all.tags("head")).item(null, 0);
                IHTMLScriptElement scriptObject = (IHTMLScriptElement)document.createElement("script");
                scriptObject.type = @"text/javascript";
                scriptObject.text = @"(
    
    function lucas() {
        var inp = document.createElement('input');
        inp.id = 'PartNumberTextBox';
        inp.style.position = 'absolute';
        inp.style.right = '6px';
        inp.style.top = '435px';
        inp.style.width = '280px';
        inp.style.backgroundColor = '#000494';
        document.body.appendChild(inp);
        var nova = document.getElementById('PartNumberTextBox');
        var so = document.getElementById('so_id');
        var newsn = document.getElementById('new_sn');
        var button = document.getElementById('book_button');
        var newpart = document.getElementById('new_part_no');
        document.body.onkeyup = function (e) {
          if (e === null) {
            e = window.event;
            var code = e.keyCode || e.which;
            if (code === '9') {
                if (e.srcElement === newsn) {
                    so.focus();
                } else if (e.srcElement === so) {
                    button.focus();
                } else {
                    nova.focus();
                }
            }
          }
        };
    
        nova.onchange = novafunc;
        nova.onblur = novafunc;
        nova.onkeyup = novafunc;
        nova.onmouseup = novafunc;
    
        function novafunc() {
            if (newpart.value === nova.value && nova.value.length > 0) {
                nova.style.backgroundColor = '#0f0';
                newsn.focus();
            } else {
                nova.style.backgroundColor = '#f00';
            }
        }
    
    });
    lucas()";
     ((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
            }
    I'm adding the script in the head ...

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Well no wonder!

    When you put that code in the <head>, *NONE* of those elements that you are trying to get using document.getElementById() *EXIST* yet!!!

    On top of that, you misunderstand the point of the
    Code:
    (
       function lucas() { }
    )();
    outermost parentheses in that coding form. The first pair of parentheses *encapsulate* the function within them. The second pair then *INVOKE* the encapsulated function *IMMEDIATLY*. Your external call to lucas( ) is a waste of time and code; the call to that function has already been performed by the last ();

    But in any case, this will never work, as given, if you inject it into the <head>, as is.

    You have two choices: The better choice is to stop using C# code to inject the JavaScript. Instead, simply put it into your page *JUST BEFORE* the </body> tag. You can either simply drop it inline into your ".aspx" page or you can put it in separate ".js" file and then drop a reference to it (e.g., <script type="text/javscript" src="lucas.js"></script>) just before the </body>.

    The not-as-good choice is to lose the parentheses shown in red and magenta above, completely.

    And then, in place of your direct call to lucas( ), use window.onload = lucas;
    That way, your function will not be called until the page is completely loaded and so all the elements you are using the ID's of will then exist.

    CAUTION: If you have other JavaScript on the page, it might already be using window.onload and so you may then cause it to not work or perhaps it will override your use on onload. If that's the case, you will have to attach your lucas function as an event handler to the load, instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. Users who have thanked Old Pedant for this post:

    Lucas Azevedo (06-24-2013)

  4. #18
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Well no wonder!

    When you put that code in the <head>, *NONE* of those elements that you are trying to get using document.getElementById() *EXIST* yet!!!

    On top of that, you misunderstand the point of the
    Code:
    (
       function lucas() { }
    )();
    outermost parentheses in that coding form. The first pair of parentheses *encapsulate* the function within them. The second pair then *INVOKE* the encapsulated function *IMMEDIATLY*. Your external call to lucas( ) is a waste of time and code; the call to that function has already been performed by the last ();

    But in any case, this will never work, as given, if you inject it into the <head>, as is.

    You have two choices: The better choice is to stop using C# code to inject the JavaScript. Instead, simply put it into your page *JUST BEFORE* the </body> tag. You can either simply drop it inline into your ".aspx" page or you can put it in separate ".js" file and then drop a reference to it (e.g., <script type="text/javscript" src="lucas.js"></script>) just before the </body>.

    The not-as-good choice is to lose the parentheses shown in red and magenta above, completely.

    And then, in place of your direct call to lucas( ), use window.onload = lucas;
    That way, your function will not be called until the page is completely loaded and so all the elements you are using the ID's of will then exist.

    CAUTION: If you have other JavaScript on the page, it might already be using window.onload and so you may then cause it to not work or perhaps it will override your use on onload. If that's the case, you will have to attach your lucas function as an event handler to the load, instead.
    The problem is that i need to use c# because it's a BHO ... i think that the problem is because of the whole website is made by frames ...Soo Javascripts will not work with frames ??? In other pages it's working ( like google , yhaoo ,etc ) but with websites with frames it doesn't work

  5. #19
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Old Pedant , i can't do that because i don't have direct acess to the code of the page ... i have done what you said

  6. #20
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Old Pedant , this isn't working on jsfindle

    http://jsfiddle.net/ENfSu/

  7. #21
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    OF COURSE it does NOT WORK!

    Your <input> fields are
    Code:
    <input id="newsn" .../>
    <input id="newpart" .../>
    Your code does:
    Code:
    var newsn = document.getElementById('new_sn');
    var newpart = document.getElementById('new_part_no');
    Your code then of course finds NO MATCHES for the ID's it is looking for!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  8. #22
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    This works:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <input id="PartNumberTextBox" style="position: absolute; background-color: yellow; width: 280px; top: 435px; right: 6px;"/>
    <input id="newsn" style="position: absolute;width: 280px; top: 300px; right: 6px; background-color: pink;"/>
    <input id="newpart" style="position: absolute; width: 280px; top: 200px; right: 6px; background-color: lightblue;" value="xyz" />
    
    
    <script type="text/javascript">
    var nova = document.getElementById('PartNumberTextBox');
    var newsn = document.getElementById('newsn');
    var newpart = document.getElementById('newpart');
    nova.onchange = novafunc;
    nova.onblur = novafunc;
    nova.onkeyup = novafunc;
    nova.onmouseup = novafunc;
    
    function novafunc() {
        if (newpart.value === nova.value && nova.value.length > 0) {
            nova.style.backgroundColor = '#0f0';
            newsn.focus();
        } else {
            nova.style.backgroundColor = '#f00';
        }
    }
    </script>
    </body>
    </html>
    Notice that the value= for the "newpart" is "xyz".

    Type "xyz" into the bottom text field (your "PartNumberTextBox") and watch its background color go red on the "x", stay red on the "y" but turn green on the "z".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  9. Users who have thanked Old Pedant for this post:

    Lucas Azevedo (06-25-2013)

  10. #23
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    This works:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <input id="PartNumberTextBox" style="position: absolute; background-color: yellow; width: 280px; top: 435px; right: 6px;"/>
    <input id="newsn" style="position: absolute;width: 280px; top: 300px; right: 6px; background-color: pink;"/>
    <input id="newpart" style="position: absolute; width: 280px; top: 200px; right: 6px; background-color: lightblue;" value="xyz" />
    
    
    <script type="text/javascript">
    var nova = document.getElementById('PartNumberTextBox');
    var newsn = document.getElementById('newsn');
    var newpart = document.getElementById('newpart');
    nova.onchange = novafunc;
    nova.onblur = novafunc;
    nova.onkeyup = novafunc;
    nova.onmouseup = novafunc;
    
    function novafunc() {
        if (newpart.value === nova.value && nova.value.length > 0) {
            nova.style.backgroundColor = '#0f0';
            newsn.focus();
        } else {
            nova.style.backgroundColor = '#f00';
        }
    }
    </script>
    </body>
    </html>
    Notice that the value= for the "newpart" is "xyz".

    Type "xyz" into the bottom text field (your "PartNumberTextBox") and watch its background color go red on the "x", stay red on the "y" but turn green on the "z".
    You are awesome Thanks

  11. #24
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Old Pedant ... How can i add html code to a frame using Javascript ?

  12. #25
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Are the contents of the frame from the same server as the page that will be doing this?

    (And do you mean <frame> or <iframe>?)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  13. #26
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Are the contents of the frame from the same server as the page that will be doing this?

    (And do you mean <frame> or <iframe>?)
    When i mean frame is really frame... this are the only tags on the webpage:


    <html><head><title><form><frameset id ="fs" ><frame id=frmMenu><frame id="frmMain"><noframes/>


    Yes the content are from the same page

    Code:
    <frame name="frmMenu" id="frmMenu" src="Menu.do?ap=eRepair&mo=WEB+REPAIR&fu=3rdParty+KP+Exchange" frameBorder="0" marginWidth="0" marginHeight="0" noResize="" scrolling="no">
    Code:
    <frame name="frmMain" id="frmMain" src="../../rmaweb/repair/rm3rdPartyKpExchangeAction.do" frameBorder="0" marginWidth="0" marginHeight="0" scrolling="auto">

  14. #27
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Sorry no <form> ...

  15. #28
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    So from the <frameset> page, just use
    Code:
    var framedoc = document.getElementById("frmMenu").document;
    // and then you can do things such as
    var div = framedoc.getElementById("someID");
    From inside the "frmMain" page:
    Code:
    var framedoc = window.parent.document.getElementById("frmMenu").document;
    // and then you can do things such as
    var div = framedoc.getElementById("someID");
    I hope you know that <frame>s are considered obsolescent at best, obsolete by many.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  16. Users who have thanked Old Pedant for this post:

    Lucas Azevedo (06-27-2013)

  17. #29
    New Coder
    Join Date
    Jun 2013
    Posts
    22
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    So from the <frameset> page, just use
    Code:
    var framedoc = document.getElementById("frmMenu").document;
    // and then you can do things such as
    var div = framedoc.getElementById("someID");
    From inside the "frmMain" page:
    Code:
    var framedoc = window.parent.document.getElementById("frmMain").document;
    // and then you can do things such as
    var div = framedoc.getElementById("someID");
    I hope you know that <frame>s are considered obsolescent at best, obsolete by many.

    Soo .. to add Textbox on the frame frmMain is :

    Code:
    var framedoc = window.parent.document.getElementById("frmMain").document;
    var body = framedoc.getElementById("body");
    var inp = body.createElement('input');
    inp.style.position = 'absolute';
    inp.style.right = '6px';
    inp.style.top = '435px';
    inp.style.width = '280px';
    inp.style.backgroundColor = '#000494';
    body.appendChild(inp);
    Updta: Didn't work :/ I'm starting to getting worry..

    Code:
    <TITLE>Repair</TITLE>
    <FORM method=post name=MainForm action=/pf/Main.do><INPUT name=target type=hidden><INPUT name=tarids type=hidden><INPUT name=menuid type=hidden><INPUT name=pfsel type=hidden><INPUT name=cu type=hidden><INPUT name=ca type=hidden><INPUT name=ap value=eRepair type=hidden><INPUT name=mo value="WEB REPAIR" type=hidden><INPUT name=fu value="3rdParty KP Exchange" type=hidden></FORM>
    <SCRIPT type=text/javascript>var framedoc = window.parent.document.getElementById("frmMain").document;
    var body = framedoc.getElementById("body");
    var inp = body.createElement('input');
    inp.style.position = 'absolute';
    inp.style.right = '6px';
    inp.style.top = '435px';
    inp.style.width = '280px';
    inp.style.backgroundColor = '#000494';
    body.appendChild(inp);
    </SCRIPT>
    <FRAMESET id=fs frameSpacing=0 border=0 cols=* frameBorder=NO rows=82,*><FRAME id=frmMenu noResize marginHeight=0 src="Menu.do?ap=eRepair&amp;mo=WEB+REPAIR&amp;fu=3rdParty+KP+Exchange" frameBorder=0 name=frmMenu marginWidth=0 scrolling=no><FRAME id=frmMain marginHeight=0 src="../../rmaweb/repair/rm3rdPartyKpExchangeAction.do" frameBorder=0 name=frmMain marginWidth=0><NOFRAMES></NOFRAMES></FRAMESET>
    Last edited by Lucas Azevedo; 06-27-2013 at 10:51 AM.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •