Extract from a page path or url excluding a specific format - extract

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.

Related

Displaying URL parameter values as text in an element on webpage (Question from a non-programmer)

I don't have any expertise in programming, just from the little I've researched to solve one-off needs. However, the one thing I can't seem to find answers for is populating dynamic content on a page.
I currently build my website with no code programs (Airtable as my back-end).
For my work, my clients each receive a link to a webpage that contains a few client-specific variables (name, birthdate, pdf link) - is it possible to pass these parameters through the URL to have them show on the webpage?
I assumed it would function similarly to passing form data to a thank you page URL, is this theory correct? And if so, can anyone help with what I need to do/what html & js codes I need to implement to make this happen?
Thanks in advance (this would make my life a million times easier)!
Yes you can easily parse URL parameters with JS, see simple example bellow:
https://exampleurl.com/?birthdate=22091977&pdflink=yourlink123.pdf
<html>
<body>
<span id="birthdate"></span>
</body>
</html>
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const birthdate = urlParams.get('birthdate');
const pdflink = urlParams.get('pdflink');
document.getElementById("birthdate").innerHTML = birthdate;
</script>
you can completely pass parameters via URL. eg: your_domain.com/name1/birth-date/code-pdf

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.
:)

retrieving URLs from functions within HTML (python)

I need to scrape some URLs from some retailer product pages, but the specific URLs I need to get aren't in the html part of the page. The html looks like this for each of the items on which one would click to get to the page with the URL I need to grab:
<div id="name" class="hand bold" onclick="AVON.productcontrol.Go(45714);">ADVANCE TECHNIQUES Color Protection Conditioner Bonus Size</div>
I wrote the following to get URLs from the page, but since the actual URLs I need don’t seem to be stored in the page, it doesn’t get what I need:
def getUrls(URL):
"""input: product page url
output: list of urls to products
"""
connection = urllib.urlopen(URL)
dom = lxml.html.fromstring(connection.read())
selAnchor = CSSSelector('a')
foundElements = selAnchor(dom)
urlList = [e.get('href') for e in foundElements]
return urlList
Is there a way to get the link that the function after ‘onclick’ (I guess AVON.productcontrol.Go(#);) takes you to? I don’t fully understand html, and while I’ve read a bit about onclick, I can’t figure out how the function after 'onclick' works.
In order to find the URL that you are taken to on click, you need to find the JavaScript source code of the 'Go' function and read and understand it. It's buried somewhere within a tag or some JavaScript .js file that is referenced directly or indirectly by the HTML page. Happy digging!
Or: you automate the interaction with the web page with a tool like Selenium (http://docs.seleniumhq.org/) and just check where it takes you if you click.

Getting a Parameter on The URL

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.