Search code examples
javascriptjquerygreasemonkeyuserscripts

Need help rewriting a url with JavaScript/Jquery


I have a userscript (http://userscripts.org/scripts/show/179402) that I'm writing that adds a bar to Google Sites like the one they removed. I'm needing the script to take the value of the search field and add it to a url (Replacement URL) and have it replace the url (Original URL) on the bar. In other words, I need to update it where the search term carries over to other pages, like the original google bar they removed.

I've tried this a few different ways. One way I tried was getting the value this way. Which, gets the value fine.

$('#gbqfq').keyup(function() {
var searchterm = this.value;
});

Then I've tried to add the search term to a url that replaces the original URL this way

var url1search = "https://www.google.com/search?q="+searchterm;
$('#url1').attr("href", url1search);

How do you replace a url with a new url plus a variable?

I'm very new to JavaScript, I'm making this script to try to learn it. If someone can help me figure out how to do this I would appreciate it very much.


Solution

  • Ah sorry, I see your problem. searchterm is only defined inside the anonymous function, it would be undefined elsewhere. Try moving rest of the script inside that function too.