Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-16-2012, 04:41 AM   PM User | #1
samuvk
New Coder

 
Join Date: Nov 2011
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
samuvk is an unknown quantity at this point
Using code by calling a function

Can I create a code in some way that I can use the same code by just calling it?

I'll explain myself better.

I have this code to show a selector.

Code:
<select id="mySelect" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option>Select an option</option>
<option value="http://www.google.com">Orange</option>
<option value="http://www.google.com">Pineapple</option>
<option value="http://www.google.com">Banana</option>
</select>
I want to use the same code in all the pages of my site, but I don't want to copy and paste the code.

Could I do something like giving a name to the script let say: MySelect

and the just do something like: call MySelect

so it will write the selector whatever I write call Myselect or something like that?

It would work a solution in Javascripg, .css or JQuery

Thank you so much
samuvk is offline   Reply With Quote
Old 12-16-2012, 10:33 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Yes, it is called a function, just as you mention in your title(?).
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-16-2012, 10:52 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Does he mean he wants to remove the script from inline and place it in a separate function?

Code:
<select id="mySelect" onchange="changeURL(this)">
<option value = "">Select an option</option>
<option value="http://www.google.com">Orange</option>
<option value="http://www.google.com">Pineapple</option>
<option value="http://www.google.com">Banana</option>
</select>

<script type = "text/javascript">
function changeURL(which) {  // note that the name of the function must be unique!
var val = which.value;
if (val !="") {
window.location.href = val;
}
}

</script>

If you mean you want to include that same code in all your web pages, then indeed copy and paste is the answer.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-16-2012 at 10:55 AM..
Philip M is offline   Reply With Quote
Old 12-16-2012, 06:37 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Philip M View Post
If you mean you want to include that same code in all your web pages, then indeed copy and paste is the answer.
Or any of the many varieties of server side include.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 12-17-2012, 07:47 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I think he means that he does *NOT* want to have to write the [icode] ... onchange="..."...[/icpde] and that he wants the onchange event to automatically be added on all pages.

SAMUVK: If indeed that is what you mean, I'd suggest using a CSS class name to indicate which <select>s you want to attach the code to.

Code:
<select class="urlSelector">
...
</select>
And then have a "urlSelector.js" file that you include on each page.

If you include the file *JUST BEFORE* the </body> of the page you can make it completely unobtrusive.

In other words, your overall page might be something like:
Code:
<html>
<head>
    ...
</head>
<body>
    ...
    <select class="urlSelector">
        <option>Select an option</option>
        <option value="http://www.google.com?q=orange">Orange</option>
        <option value="http://www.google.com?q=pineapple">Pineapple</option>
        <option value="http://www.google.com?=banana">Banana</option>
    </select>
    ...

<script type="text/javascript" src="urlSelector.js"></script>
</body>
</html>
And then the contents of your urlSelector.js file (or whatever you choose to name it) could be something like this:
Code:
(
  function( ) /* anonymous master function
  {
      var sels = document.getElementsByTagName("select");
      for ( var s = 0; s < sels.length; ++s ) 
      {
          var sel = sels[s];
          if ( sel.className.indexOf("urlSelector") >= 0 ) 
          { 
              sel.onchange=changeURL; 
          }
      }
      function changeURL( )
      {
          var url = this.value;
          if ( url != "" ) location.href = url;
      }
  } // end of master function
)(); // self-invoke master function
Lots of other ways to skin this particular cat, but one advantage of this one is that you could have two or more such <select>s on any HTML page, if desired.
__________________
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:11 AM.


Advertisement
Log in to turn off these ads.