How to retrieve just the loaded page's file name? - javascript

How can I check the loaded script (instead of the loaded script path) with JavaScript?
Example:
Loaded script is Index.php
If I use window.location.pathname, I get /folder1/folder2/Index.php.
If I use window.location.href, I get http://www.website.com/folder1/folder2/Index.php.
If I use window.location.hostname, I get www.website.com.
What should I use to get only Index.php?

You can split the path by / and then pop() the last item. E.g.
window.location.pathname.split('/').pop()

Yes, you manipulate the string that you have. You can use string methods to get the part after the last slash:
var page = window.location.pathname.substr(window.location.pathname.lastIndexOf('/') + 1);

Related

How to set Metatitle for a user-entered javascript hashed URL via PHP

I want to set a specific metatitle to a javascript hashed url. I want to set it via PHP so that when google crawls it the metatitle is already there.
An example of a page: https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25
In this url /en/shop/ is the wordpress page, and #!/men+long+sleeve+shirts?q=P25 is set by javascript.
I have this code in the functions.php to try to set the title:
if (is_page(194) and (strpos($url, 'men+long+sleeve+shirts') !== false))
{$new_title = "long sleevetitle";};
return $new_title;
But how do I get 'men+long+sleeve+shirts' or '?q=P25' in the $url variable?
Is there a way to get the user-entered url in PHP?
You can use combination of strpos() and substr();
Like this:
$string = "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
var_dump(substr($string,strpos($string,'?'))); // output '?q=P25'
It will cut whole string and leave only the part from ? to end.
Explode method:
Or you can use alternate, explode method like this, which will ensure that it will target only last element with question mark.
$string= "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
$string2= explode('?',$string);
var_dump(end($string2)); // output 'q=P25'
Edit:Both methods would work with "#" also, just replace '?' with '#'.
Refs:
http://php.net/manual/bg/function.strpos.php
http://php.net/manual/bg/function.substr.php
http://php.net/manual/bg/function.end.php
http://php.net/manual/bg/function.explode.php

Use URL Parameter to call a line of text using JS?

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]);
})

Javascript: add body ID based on part of search results url

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;

Get path including GET variables using javascript

I need to get the whole path of page (excluding the domain) so I can show it in an iframe.
I currently use location.pathname to get the path, but the problem is that they may appear GET variables in the URL.
So for a page like article.php?id=23 I get only article.php using location.pathname, thus the page displayed in the iframe is simply a 404 page.
Is there any function to get the path including GET variables?
There probably isn't an out of the box function, no.
But you can use this as a reference to create your own:
Mozilla DOM Reference
Specifically, using window.location.pathname (strip the leading "/" if you don't want it) and window.location.search to get the querystring.
i.e
function whatIWant()
{
return window.location.pathname.substring(1) + window.location.search;
}
window.location.search.replace( "?", "" );
this will return the get variables.
LINK=http://www.javascriptkit.com/jsref/location.shtml
Answer to your question->no,there is no any built in function ,we have to make our custom function and parse it.
Get escaped URL parameter

How to Extract the Current URL and ID in jQuery?

How would I go about getting the current URL using jquery, or more specifically, getting an ID on the end of it?
For example, I have product.php#tab-2. What I want to get from it is just the '#tab-2' part.
I have tried 'window.location.pathname' but that will only return '/product.php'
Thanks
You don't need jQuery for this:
alert(window.location.href); // will give you the full url
alert(window.location.hash); // will give you the hash (#) value
See the Mozilla docs at window.location - MDC.
You want window.location.hash
Use jqUrl plugin (http://www.oakcitygraphics.com/jquery/jqURL/jqURLdemo.html) to retrieve the full current url, then strip the window.location.pathname part.
Thomas
To actually assign to a variable, use the following;
var url = window.location.href;
var id = url.substring(url.lastIndexOf('#') + 1);
Note that if your url is in this format /product.php/3 , then you can use the above code, just change the character is the lastIndexOf function.

Categories