I'm hoping to call a line of text by using a simple URL parameter. Say I had an ordered list in javascript and on load of url example.com/?i=14 would get the 14th line in my list and place it where desired.
How can I achieve this?
I'm not sure what you mean by "call a line of text," but maybe you could do this:
var url = window.location.href;
var queryPos = url.indexOf('i=');
var param = url.substr(queryPos + 'i='.length);
Now param will contain the value of the parameter and you could use it to fetch whatever.
But since you're trying to access a value from a URL with JavaScript, it might be better to make use of # as explained here: How do I get the value after hash (#) from a URL using jquery (there are non-jquery answers as well)
Hopefully this is what you need.
To place an array element where you need it on document load
<div id="placeHere"></div>
In JS
document.body.onload = function(){
document.getElementById('placeHere').innerHTML = array[14];
}
jQuery
$(document).ready(function(){
$('#placeHere').html(array[14]);
})
Related
I really hope to find a solution here.
Need to load specific elements highlighted first on the pages based on url hash.
I have already set up "click" and "hover" functions for these elements. But also need these elements highlighted based on url. What selector should I use?
Basically I need the following scenario to be implemented:
if https://mypage.com#case1 loads
do this
if https://mypage.com#case2 loads
do this
If I understand your question, you can get the URL and do a simple if else statement where you load what you need to based on the URL string.
It could be something like this:
var url = window.location.href; //get url string
if(url == "https://mypage.com#case1"){
//run your case1 code
}else if(url == "https://mypage.com#case2"){
//run your case2 code
}
I'm not sure what your use case is, but you probably want to parse the URL to get the relevant piece or parameter you are looking for.
I'm expecting URLs in this for:
/user/username
but end users can also add whatever get parameters they want, like so:
/user/username?foo=bar
With that said, using AngularJS, what's the preferred way for me to get just the username (which appears after /user/) without anything else after it?
You should use the $location service and its .path() method, then use a regular split() and indexing.
I doubt there's a dedicated function for it but it seems easy enough to pull it out of the string with the query.
Get the path with $location.path() and then both of these does the job for you.
url.substring(6, (url.indexOf('?') != -1 ? url.indexOf('?') : url.length))
url.split('/')[2].split('?')[0]
Same question here: Is there a built-in way to get the current URL without any query parameters?
you can use window.location or $location service to get the path and then using split function
like this
var url = "/user/username?foo=bar";
var check = url.split('/user/');
var username = check[1].split('?');
console.log(username[0]);
you can find username by applying multiple split on your url. hope this is what you want.
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("&"));
I know just enough JS to get in trouble so please bear with me :)
I am using the WP-Properties WordPress plugin. When searching for properties it gives all results in a common search results page. I need to theme the search results page based on part of the search string so I need a body id.
Example of a search result url:
http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1
The part I want is what comes after: "wpp_search[property_type]"
In the above case it would be "oasis_park"
And this would then create a body tag of: <body id="oasis_park">
I tried to tweak the following code to get the specific part then have it write to the body tag but I can't get it to work in my situation: remove a part of a URL argument string in php
This will only work for this specific url, as you have not provided a general pattern for each url from which you will need to extract a substring:
var myString = "http://website.com/property/?wpp_search[pagination]=off&wpp_search[property_type]=oasis_park&wpp_search[lot_location]=Oceano+Lot&wpp_search[availability]=-1&wpp_search[phase]=-1&wpp_search[price][min]=-1";
var myVar = myString.slice(myString.indexOf("&") + 27, myString.indexOf("k"));
After you have identified a general pattern in every url you wish to check, you will then have to use a combination of substr(), slice() and indexOf() to get the substring you want.
Then, try
document.body.id = myVar;
or assign an id to body (e.g. "myID") then try this:
document.getElementById('myID').id = myVar;
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");