Need help rewriting a url with JavaScript/Jquery - javascript

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.

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.

Related

Link to a modal window from URL

Title says it all, I would like to trigger a jQuery event that opens a unique model window depending on which URL is used. I've looked at a few solutions and all of them seem to require bootstrap which I am not using or simply don't seem to work for me.
I think i understand the logic, I'm just not sure how to actually code it and would be grateful for some help. Here is my thinking:
[STEP 1]
On page load, check the URL.
If the the url is normal e.g. "www.domain.com/example", don't do anything.
If the url has a substring on the end e.g. "www.domain.com/example/#red", "www.domain.com/example/#green", or "www.domain.com/example/#blue" etc., set that substring to a variable. In this case the variable would equal either red, green, or blue.
[STEP 2]
Insert the variable where the line of code says [color] and execute.
$("document").ready(function() {
$(".details, #[color]details").trigger('click');
});
use with window.location.hash. its will get the hash value form url with# .so no need to add # in the dom
$("document").ready(function() {
if(window.location.hash.trim().match(/(\w+)/)){
$(".details,"+window.location.hash+"details").trigger('click');
}
});
You can use document.referrer to get the Page URL.
Store it in a variable to fetch the last segment or the URL using substr().
Then check it in conditional operator if the last part is your desired text, add it to your class and trigger.
I ll paste my code which I used on the next page to trigger tab change for some requirement. I hope this will work for you too, hopefully. Thank you.
$(document).ready(function () {
var referrer = document.referrer; // Get the Url of the previous page
var lastPathSegment = referrer.substr(referrer.lastIndexOf('/') + 1); // extracts the last part e.g. the page name
if(lastPathSegment == "invoices.php"){
customer_detail_content();
$('a[href="#tab_6_2"]').trigger('click');
}

Passing variables to Javascript from "pretty" URL

My problem is, I would like to create "pretty" URLs for visitors that look like this:
http://domain.com/Name
I have users that often send friends to my service, and I have been created customized pages for each one with the person's First Name in the headline. E.g., "John, here's an easy way to fix this widget"
I then save the page as an index.html file in a custom folder so the link structure for the custom page is domain/Name with Name being their First Name.
This is getting tedious and I would love to use Javascript to automate the process. However, the only documentation I can find on passing variables to Javascript involves "ugly" domains such as domain/jspass2.html?FirstName=John&LastName=Smith
Is there a way to beautify these domains and still pass the variables to a javascript code that inputs their name into the html code? I don't want to "cloak" an ugly domain (using a href, for example)
Thanks for the help!
Well, you could make it "prettier" by making the querystring cleaner.
example:
http://www.domain.com/?John,Smith
The javascript in your index file can read that.
var getQueryString = function() {
queryString = window.location.search;
queryStringCleaned = queryString.substring(queryString.indexOf('?') + 1 );
return queryStringCleaned;
};
if "http://domain.com/Name" is your domain, variable firstName will have the value "Name".
var firstName = window.location.pathname.match(/([\w-]+)\/?.*/)[1];
You could just take the whole URL in JS, and parse it "by hand". Use this regex (for example) to find the parameters passed.
In addition to Paul, I wrote you something that extracts the first name field from the url you provided. If the format is consistent, and you can obtain the url in javascript, you can use this. You may possibly have to create the page first, then redirect the user because javascript is a client side language and the page will already be rendered.
var url = "domain/jspass2.html?FirstName=John&LastName=Smith";
url = url.slice(url.indexOf("FirstName=") + 10, url.length);
url = url.slice(0, url.indexOf("&"));

Edit and search an URL with JS

I'm trying to make an easy google chrome extension that gets a string from a textbox and adds it to an URL (ie "https://www.google.com/search?q=") and then google the URL+ input string.
How should i make it?
i must say that i'm a beginner, i was thinking of a function that reads the input from the textbox and adds it to the URL. something like this: var google = "google.com/search?q=" + textbox
Something like this maybe ?
Getting the content of the textbox (assuming the target input has a textbox id ofc):
var query = document.getElementById('textbox').value
Redirection:
window.location.href = 'https://www.google.com/search?q=' + query
I'm not quite sure of what you'd exactly like though.
You can use the omnibox api in order to get what you need. I've wrote a detailed explanation on this solution here: http://greenido.wordpress.com/2013/03/28/chrome-extension-for-enterprise-internal-usage/
Btw, I hope you gave google.com search just as an example... because it's the default behavior :)

Replace end characters of current URL with bookmarklet

Is there a way to replace all characters after the last backslash in the currentURL with another string via javascript bookmarklet?
I'm doing a lot of auditing work with Sharepoint sites and having to manually look at the settings pages for sites by entering strings to the end of a URL. For example, I might go to a site like:
https://site.com/..../default.aspx
And I replace the "default.aspx" with "_layouts/user.aspx" and reload the new page so it is now at:
https://site.com/..../_layouts/user.aspx
It's not always "default.aspx", so I can't just use a simple string replace. I know there is a way to manipulate the URL via a javascript bookmarklet, but my knowledge of how to do that is limited at best. Any help or guidance would be greatly appreciated
I don't know if this is what you thought, but if you just want to change the last part of the url with something else, you could use this bookmarklet
javascript:(function(){
var curloc = document.location.href.split('/');
var urlEnding= '/_layouts/user.aspx';
curloc = curloc.splice(0,curloc.length-1).join('/')+urlEnding;
document.location.href = curloc;
})();
You could replace the fixed url with
prompt('Enter your url:', '_layouts/user.aspx');
if you need to change the last part each time.
I hope this helps.

Javascript & HTML Getting the Current URL

Can anyone help me. I don't use Client-side Javascript often with HTML.
I would like to grab the current url (but only a specific directory) and place the results between a link.
So if the url is /fare/pass/index.html
I want the HTML to be pass
This is a quick and dirty way to do that:
//splits the document.location.href property into an array
var loc_array=document.location.href.split('/');
//have firebug? try a console.log(loc_array);
//this selects the next-to-last member of the array.
var directory=loc[loc.length-2]
url = window.location.href // Not particularly necessary, but may help your readability
url.match('/fare/(.*)/index.html')[1] // would return "pass"
There may be an easier answer, but the simplest thing I can think of is just to get the current URL with window.location and use some type of parsing to get which directory you are looking for.
Then, you can dynamically append the HTML to your page.
This may get you started:
var linkElement = document.getElementById("whatever");
linkElement.innerHTML = document.URL.replace(/^(?:https?:\/\/.*?)?\/.*?\/(.*?)\/.*?$/i,"$1");

Categories