Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member prophil's Avatar
    Join Date
    Aug 2007
    Posts
    4
    Member #
    15766
    Iím trying to write a function in wich the parameter is taken into a condition. Depending on whatís written as a parameter the function will write stuff into the HTML page. Iíll write you a phony script so that you can get the picture.

    SCRIPT JAVASCRIPT
    FUNCTION dynamic(PARAM)
    if PARAM = hello
    document.write(ĎIím writing a text to say helloí)
    if (or else if) PARAM = bye
    document.write(ĎIím now saying byeí)

    I know it must be pretty simple, but I tried lots of different structures and I searched all over the free stuff on the Internet but I found nothing that can help me with my problem. Can anybody suggest me a workable script that could do that kinda job?

    Also just to be sure, how should I call up the function in BODY?

  2.  

  3. #2
    Senior Member seanmiller's Avatar
    Join Date
    Sep 2003
    Location
    Glastonbury, UK
    Posts
    868
    Member #
    3263
    Liked
    1 times
    I'm assuming there is some complexity I'm missing, but on face value you're just after something like this...

    Code:
    <html>
    <head>
    <script type="text/javascript">
            function test(p1) {
                    if ( p1 == "hello" )
                            alert("I just called to say Hello");
                    else if ( p1 == "goodbye" )
                            alert("You have to go? Shame...");
                    else
                            alert("Sorry, didn't understand that at all!");
            } 
    </script>
    </head>
    <body>
            <input type="submit" onclick="javascript: test('hello')" value="Hello">
            <input type="submit" onclick="javascript: test('goodbye')" value="Goodbye">
            <input type="submit" onclick="javascript: test('asdkjasd')" value="Waffle">
    </body>
    </html>
    That sort of thing?

    Another variation of the theme, this time acting on the value of a <select> box...

    Code:
    <head>
    <script language="javascript">
            function test(p1) {
                    if ( p1.value == "hello" )
                            alert("I just called to say Hello");
                    else if ( p1.value == "goodbye" )
                            alert("You have to go? Shame...");
                    else
                            alert("Sorry, didn't understand that at all!");
            } 
    </script>
    </head>
    <body>
            <select name="option" onchange="javascript: test(this)">
                    <option value="hello">Hello
                    <option value="goodbye">Goodbye
                    <option value="asdjasdkas">Waffle
            </select>
    </body>
    </html>
    Hope that helps,

    Sean

  4. #3
    Junior Member prophil's Avatar
    Join Date
    Aug 2007
    Posts
    4
    Member #
    15766
    Thank you for your good support Sean, but see, I really need to write a document.write type of statement because really, I need to write html into BODY. Look, here's a code that I've tried and didn't work (I put it into a js file and I tried to call it up into the BODY of my Webpage. Could you tell me what's seems to be wrong here :

    function test(p1) {
    if ( p1 == "hello" )
    document.write('<a href="http://www.webdesignforums.net/">Hello there!</a>');
    else if ( p1 == "goodbye" )
    document.write('<a href="http://www.trash.com/">Ok... So Goodbye! </a>');
    else
    document.write('Sorry, didn't understand that at all!');
    }

    Now into BODY :

    <script language="javascript" src="test.js">test('hello')</script>

  5. #4
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    Seeing that you want a very JS-heavy function, I suggest you look into a JS library which can do these things much easier for you. Mine of choice is jQuery. There are tutorials that can help you jump straight into this stuff.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:40 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com