![]() |
Simple javascript needed.... Can any JS experts help me with what I think is a pretty easy script? What I need is an input box. User inserts a word/phrase into box and presses "go". The word/phrase is than added to multiple URLs on the webpage, replacing some type of token. When replacing the token, any blank spaces between keywords needs to be replaced with either: + or %20 Example..Page has following URLs: www.Google.com/search=[keywords] www.home.com/[keywords].html Basically, it's really just a simple search/replace function, with the "find" always [keyword] but needs to be broswer based and fill in the blank spaced between keywords. Thanks in advance... |
Re: Simple javascript needed.... Hi, Kurt. Try this: HTML Code: <html>HTH. Steve |
Re: Simple javascript needed.... Wow Steve, Thanks a lot..l.And the reset button was something I didn't think about at all, but now see why it's needed. |
Re: Simple javascript needed.... Quote:
Quote:
Steve |
Re: Simple javascript needed.... thats a really handy script. thanks! |
Re: Simple javascript needed.... Hey guys, I know you needed just a simple script so it may not be an option for you but you could use jQuery and do all of Steve's functionality in about 2 lines. If you're interested, I can post some sample code. |
Re: Simple javascript needed.... I don't know about anyone else, but I for one would be delighted to see how to do this using jQuery. It's one of the next things on my to-learn list. Thanks. Steve |
Re: Simple javascript needed.... Hey Steven, I put together a quick script. It looks a little intense because of jQuery's awesome chaining abilities but I will break down each part. BTW, if you like javascript, you will fall in love with jQuery. I am an addict. Ok, so for this example, I only did the replacement portion to give an idea of how to do this. Here's the code: The jquery portion: Code: <script src="jquery.js"></script>Code: <input type="button" id="btnGo" value="Go"/>I attached a file with the basic example. Keeping in mind that the anchor tag has a class of "keyword", let's break this down: This class of "keyword" is how we will make the anchor tags accessible to jQuery. You could use other methods but that's another story. So for the jQuery portion. Lets break it down from start to finish. Code: $(document).ready(function(){Code: $('#btnGo').click(function(e){Code: e.preventDefault();Now, if you're a nerd like me, is when it gets cool. Code: $('.keyword').each(function(){$(this).attr('href',$(this).attr('href').replace('[keywords]','sample')).html($(this).html().replace('[keywords]', 'sample'));Each is a built loop that will do as it suggests, find each one and perform a custom function. Code: $(this).attr('href',$(this).attr('href').replace('[keywords]','sample')).html($(this).html().replace('[keywords]', 'sample'));So, though it was actually more than 2 lines, you can see how jQuery can really reduce code and provide a nice elegant way to do things (once you get the nuances down). Hope that was fun ;) Dave |
Re: Simple javascript needed.... Sorry for taking so long. It took forever getting this post past the regular expressions. I also had some trouble attaching the file. If youre interested in it, PM me. |
Re: Simple javascript needed.... Hi, Dave. Thanks for the jQuery tutorial! Very interesting. Looks like powerful stuff, but the syntax isn't exactly intuitive, is it? I'll have to do some tinkering to get comfortable. I'll PM you my email so you can send the file. Steve |
| All times are GMT -6. The time now is 08:30 PM. |