Reply
Simple javascript; changing onkeypress (At least it sould be simple)
Old 10-01-2008, 10:26 PM Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
I have two questions... the first one i have the webpage below... yegh very ulgy.. i would like to use javascript to change the javascript:function1() to
javascript:function2() in the onkeypress in the body tag of the html...

I have tried various things such as document.body.onkeypress = "javascript:function2()" and varations on that...
can someone tell me how to do this and tell me that the document.body.onkeypress actually refers to...

Thanks Much


HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>First Question</title>
        <script type="text/javascript">
        function1() { alert('function1'); }
        function2() { alert('function2');}
       </script>
    </head>
    <body onkeypress="javascript:function1()">
    </body>
</html>
Raisdead is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 10-01-2008, 10:38 PM A diffrent simple problem with javascript and frames
Average Talker

Posts: 21
Name: Taylor
I have a Simple Framed Web site that will eventually be usefull... (I hope)

I would like to retireve the innerHTML of a ID ('footer') in website in a diffrent frame Thesse are srcs

index.html

HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
   "http://www.w3.org/TR/html4/frameset.dtd">
<html>
    <head>
        <title>Spiders ewww...</title>
    </head>
    <frameset rows="1%,99%">
        <frame name="crkmes" src="removed software cracking site url" />
        <frame name="mypage" src="top.php" />
    </frameset>
</html>
top.php
HTML Code:
<SCRIPT language="JavaScript">
    function exec(){
  var html = parent.crkmes.document.getElementById('footer').innerHTML;
        alert(html);
    }  
</SCRIPT> 

<body">
<div id="test"><span id="archive"><img class="d"  /></span></div>

<a href="" onmouseover="javascript:exec();">test</a>

</body>
Thanks for trying to help... and thanks for helping if you do help

Last edited by chrishirst : 10-02-2008 at 09:05 AM.
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 06:35 AM Re: A diffrent simple problem with javascript and frames
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
Frames are very very very rarely useful and if the site "useful" but is in frames, it negates it's usefulness by making it unusable.

BUT only javascript function calls in the href attribute needs to be prefixed with "javascript:"
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 06:36 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
same answer as in your other question.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 08:39 AM Re: A diffrent simple problem with javascript and frames
Average Talker

Posts: 21
Name: Taylor
I changed the
<a href="" onmouseover="javascript:exec();">test</a> to
<a href="" onmouseover="exec();">test</a>

and it is still unopperational

thank you for that piece of information though
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 08:45 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
I tried removing the javascript: on the onkeypress to just the function name and the


document.body.onkeypress = "function2()";

still does chnage the onkeypress in the <body> tag
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 09:07 AM Re: A diffrent simple problem with javascript and frames
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
which browser? any error generated?

link? ( remove the reference to the illegal site first)
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 09:08 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
both threads on same topic merged
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 09:14 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
functions are defined as

function functionName(parameters) {
actions to be done by the function;
}
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 07:34 PM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
this is the full html

index.php
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Apple IIe</title>
        <script type="text/javascript" src="Scripts/functions.js"></script>
    </head>
    <body onkeypress="contents()">
        <div id="main">
            <span id="loading">apple //e</span>
        </div>
    </body>
</html>
Scripts/functions.js
Code:
    function contents() {
        document.getElementById("main").innerHTML = '<div>HTML CUT FOR    UNDERSTANDING</div>' ; 
        document.body.onkeypress = "vcredits()";
    }
    function vcredits() {
         alert('2');
    }
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 07:35 PM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
Firefox; chrome; ie 5-7; no error...
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 09:20 PM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
javascript is case sensitive

onkeypress should be onKeyPress
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-02-2008, 10:44 PM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
I changed the onkeypress on the javascript to onKeyPress and i still never get the vcredits() function to replace the contents() function on the <body onkeypress="contents()"> (proff is that i never get the alert('2'); message when i press a key...)

if this helps

current working html

HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>Apple IIe</title>
        <script type="text/javascript">
           function contents() {
          alert('contents');
            document.getElementById("main").innerHTML = '<div>HTML CUT FOR    UNDERSTANDING</div>' ; 
            document.body.onKeyPress = vcredits();
        }
        function vcredits() {
            alert('2');
        }
       </script>
    </head>
    <body onkeypress="contents()">
        <div id="main">
            <span id="loading">apple //e</span>
        </div>
    </body>
</html>

Last edited by Raisdead : 10-03-2008 at 09:13 AM. Reason: Changing html to show error in next post
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-03-2008, 05:11 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
the function name should NOT be in quotes.

You are assigning a method to the event NOT a string
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-03-2008, 09:12 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
This executes the function right away I want it to wait till i press a key again other wise i would just put the function at the end of the contents() function...

I know i didnt say that in my message but now you know

i edited the html in the message above to represent the change





This might help or might not...


I put the following line in the beginning of the contents() function

alert(document.body.onKeyPress);

i received an alert that said undefined the first time

then every time i press a key on the same page it says vcredits()

Last edited by Raisdead : 10-03-2008 at 09:17 AM. Reason: adding content
Raisdead is offline
Reply With Quote
View Public Profile
 
Old 10-03-2008, 09:38 AM Re: Simple javascript; changing onkeypress (At least it sould be simple)
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
Quote:
I want it to wait till i press a key again other wise i would just put the function at the end of the contents() function...
For that you need to use the onKeyUp event to set the body.onKeyPress to a new function, otherwise it will execute straight away because (obviously) there is a key down.

Quote:
alert(document.body.onKeyPress);

i received an alert that said undefined the first time

then every time i press a key on the same page it says vcredits()
As it would. It is displaying the name of the function assigned to the object.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-03-2008, 05:49 PM Re: Simple javascript; changing onkeypress (At least it sould be simple)
Average Talker

Posts: 21
Name: Taylor
I still get the 2 two alerts in a row meaning that the functions are executing back to back

current code

HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en&qu