Getting a Parameter on The URL - html

It's common to use URL parameters to show dynamic information, just like here on StackOverflow:
http://stackoverflow.com/questions/1183842
But would that be possible when you're without any language like PHP or others? Is there any way to get that value using JavaScript and capture it inside a variable to present it like this:
<h1>param_variable</h1>

window.location.href contains the current URL. With some string manipulations you can grab anything you want from it.
In this case:
var url = window.location.href;
var id = url.split('/')[4];
alert(id);
Will alert
4479268

In JavaScript you can get the current page's url like this:
var url = location.href;
You may then need to parse it your self to find the parameter that you are looking for.

Related

Extract from a page path or url excluding a specific format

I am currently working on a Google Tag Manager regex formula but what it needs to do is just show a specific word/s from a page path or page url.
For example:
/page/course/tag-manager-lesson1/?
/page/course/tag-manager-topic1/?
1st Output is "tag-manager" then
2nd Output is either "lesson1" or "topic1"
I found this question closely similar but somehow a little different on what is being extracted.
Thanks for the help!
JavaScript Variable
function(){
var allMatches = Array.from("/page/course/tag-manager-
lesson1/?".matchAll(/.*(tag-manager)-(\w+)\//g))
var result = [allMatches[0][1], allMatches[0][2]]
return result;
}
This should work. Or you have other more complicated page path than this.

How do I identify and display an embedded URL parameter using HTML?

Using a redirect URL from Cognito Forms, I'm able to embed a parameter in the URL. Works great, I'm a big Cognito Forms fan! The URL looks like this: http://mywebsite.com/results?Score=x where x is equal to any whole number from 0 to 100. So when the page loads as a result of the redirect URL it might be http://mywebsite.com/results?Score=48
I simply want to display the score on my page, in this case x = 48. I'm using a GoDaddy Website builder which allows me to insert a custom HTML code section on a page, but I haven't been able to figure out the right code to make the actual Score display. I simply want to show on the page ... Score = 48 ... or whatever value is is embedded in the URL.
Seems like it shouldn't be hard, but I haven't been able to accomplish it. GoDaddy isn't able to offer any help related to HTML code. Any assistance would be appreciated.
you need javascript
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const score= urlParams.get('Score')
console.log(score);
var para = document.createElement("p");
var node = document.createTextNode(score);
para.appendChild(node);
var element = document.getElementById("div1");
element.appendChild(para);
Something like this will show the score after a div with the id 'div1'

Create a dynamic iframe that receive its URL from the address bar

I have a simple website, and I'd like to hide some URL addresses using iframe.
Is there any way to create a code that receive the ifram's URL from the address?
Something like that:
www.myschool.com/iframe?url=www.stackoverflow.com
I'm looking for a code that enables to automatically receive the iframe's URL (www.stackoverflow.com) from the address bar, to be embedded in my website.
Thanks!
You can use the split method on the string of the url and take everything after the '?' by targetting the [1] position of the resulting array, then change the iframe's 'src' attribute to that url.
var url = window.location.href.split('?')[1];
document.querySelector('iframe').setAttribute('src',url);
Try breaking the first line into two like this:
var url = window.location.href;
url.split('?')[1];
document.querySelector('iframe').setAttribute('src',url);

HTML Form to Remove ?get=info on POST Submit?

I have several pages that are arrived on with valid GET data, such as http://website.com/?id=12345
I have a generic HTML form that is pulled onto many different pages using php's "require" and submits using POST. Regardless of which page this form is located on, it should always submit back to that same page. However, after the form is submitted, I would like the ?id=12345 to be stripped out.
So, for example, if the user is on http://website.com/new.php?id=12345, it should post back to http://website.com/new.php. If the user is on http://website.com/old.php?id=12345, that same form it should post back to old.php
Previously the best solution I found was to style the form as such:
<form action="?" method="POST">
Which will change all links to http://website.com/new.php? or http://website.com/old.php? which is very close, but not perfect.
As it turns out, I finally found the solution to my problem by using JavaScript:
url = location.href;
qindex = url.indexOf("?");
This can pull whatever is on the address bar as a string and find the index of the first ? mark. From there:
if(qindex != -1)
tells me that there is a ? mark
var plainUrl = url.substring(0, qindex);
Can get, as a string, everything up to the ? mark, but not after. Finally:
window.location.replace(plainUrl);
Will rewrite the address bar to the plain URL, not including the ? or whatever comes after, and without redirecting the browser.
Since your page will not undergo any server-side processing, you can achieve what you want via a combination of the following two tricks.
First, change your particular querystring to a hash, which is thereafter directly editable without triggering a page reload:
http://yourdomain.com/page.html#search=value
Then modify such a script as this to do what you want to do, according to the query string passed in.
<script type='text/javascript'>
// grab the raw "querystring"
var query = document.location.hash.substring(1);
// immediately change the hash
document.location.hash = '';
// parse it in some reasonable manner ...
var params = {};
var parts = query.split(/&/);
for (var i in parts) {
var t = part[i].split(/=/);
params[decodeURIComponent(t[0])] = decodeURIComponent(t[1]);
}
// and do whatever you need to with the parsed params
doSearch(params.search);
</script>
now you can delete the query string suffix in the following way:
As detailed elsewhere, namely hide variables passed in URL, it's possible to use JavaScript's History API in modern browsers.
history.replaceState({}, null, "/index.html");
That will cause your URL to appear as /index.html without reloading the page
This little gem is explained in more detail here:
https://developer.mozilla.org/en-US/docs/Web/API/History_API

Grabbing some specific text from the URL of the SWF file? Action Script 3.0

OK, So I have a banner that will be place on a website and depending on the url I need to update the text in the swf banner. The text I need to grab will be something like this in the URL.
product="abc"
The "abc" could be different number of characters. I don't have the exact url to work with.
I got a partial answer over here: Get Current Browser URL - ActionScript 3
However this does not explain how I can specifically only get the name of the product.
Thanks You
You can do something like this:
//check to make sure you can use ExternalInterface
if(ExternalInterface.available){
//grab the url from JavaScript
var url:String = ExternalInterface.call("window.location.href.toString");
//make sure the url is valid and has querystring parameters (eg a ?)
if(url && url.length > 0 && url.indexOf("?") > -1){
//split the url and grab everything after the ?, convert that into URL variable object
var params:URLVariables = new URLVariables(url.split("?")[1]);
trace(params.product); //abc
}
}
For more info on getting the url properly, see the answer to this question
You need a webservice or flashvars to know what the current product.
I hope this is helpful for you.
:)