<a href='http://www.domain.com' id='replace' style='text-decoration:none;color:black;font-size:10px;'>This is text link</a>
<script language="javascript">
var newURL = "mydomain.com/?refid=4877";
onload=function() {
var dt = document.getElementById("replace");
document.body.innerHTML = dt.getAttributeNode("href").value.replace(/domain.com/g,newURL);
}
Just assign to the href attribute:
dt.href = dt.href.replace(/domain.com\/?/, newURL);
The optional trailing slash caters for browsers that automatically add a slash to hrefs.
You should probably use JQuery:
$(document).ready(function(){
$("#replace").attr("href","http://www.mydomain.com/?refid=4877");
});
Related
I am trying to parse the email ("test#test.com") from the url and insert it together with an identifier to the popupwidget url.
The end result should be "showPopupWidget('https://website.com?utm_campaign=test#test.com')".
Unfortunately, I cannot seem to get it to work. Any help would be greatly appreciated.
Thanks!
//<![CDATA[
window.onload=function(){
url = "https://url.com/abc?email=test#test.com";
var paramId = new URL(url).searchParams.get("email");
var frameElement = document.getElementById("calendlyid");
frameElement.src = "https://webite.com" + "?utm_campaign=" +paramId;
}//]]>
<h2 class="subtitle">
<a id="calendlyid" href="" onclick="iAmAttilasEvent(); Calendly.showPopupWidget('https://website.com');return false;" class="link">Book NOW</a> </h2>
Try this:
frameElement.href= "https://webite.com" + "?utm_campaign=" +paramId;
Found the answer...
//<![CDATA[
window.onload=function(){
url = document.location.href;
var paramId = new URL(url).searchParams.get("email");
var url = "iAmAttilasEvent(); Calendly.showPopupWidget('https://website.com" + "?utm_campaign=" +paramId + "');return false;";
document.getElementById("ID").setAttribute("onclick",url);
}//]]>
<a id="ID" href="" class="link">Book Now</a>
Trying to do the following:
Store params from url, i.g. mydomain.com/page.html?cid=123456
If a user clicks on a button (they have a class of .btn-cta) it will take the params ?cid=123456 and add them to the new page those buttons link to /tour.html
I'm currently doing 1/2 of that with passing the params to an iframe on the page, now I need to get the above part working:
var loc = window.location.toString(),
params = loc.split('?')[1],
iframe = document.getElementById("signupIndex"),
btn = $('.btn-cta');
iframe.src = iframe.src + '?' + params;
Here's how I'd do it using jquery:
$('.btn-cta').each(function(i, el){
let $this = $(this); // only need to create the object once
$this.attr({
href: $this.attr("href") + window.location.search
});
});
And in Vanilla ES2015
document.querySelectorAll('.btn-cta')
.forEach(el => el.attributes.href.value += window.location.search);
This takes all the elements that have class .btn-cta and appends the page query string to each of their href attributes.
So if the page url is `http://domain/page.html?cid=1234
Tour
becomes
Tour
<html>
<head>
<script src="js/jquery.js"></script>
<script>
$(document).ready(function() {
var loc = window.location.href;
var params = loc.split('?')[1];
$(".btn-cta").click(function(){
window.open("tour.html?"+params,'_self',false);
});
});
</script>
</head>
<body>
<button type="submit" class="btn-cta">Click Me</button>
</body>
</html>
current code (not working):
/^script\s*type=\"text\/javascript/i.test(tagName)
/<script\stype\=\"text\/javascript\">/i
Regular expressions and HTML — bad things. How regex will work for next examples (don't forget about single and double quotes)?
<script type="text/javascript">
<script language="JavaScript" type="text/javascript">
<script type="text/javascript" language="JavaScript">
<script class="myJS" type="text/javascript">
<script type="text/javascript" class="myJS" >
Instead of regular expressions, I suggest to use a function like this:
function attr_in_str(str, tag, attr) {
var div = document.createElement('div');
div.innerHTML = str;
var elems = div.getElementsByTagName(tag);
for (var i = 0; i < elems.length; i++) {
if (elems[i].type.toLowerCase() == attr.toLowerCase()) {
return true;
}
}
return false;
}
Then use it:
var str = 'This is my HTML <script type="text/javascript"></script>';
var result = attr_in_str(str, 'script', 'text/javascript');
Assuming that you are aware of all the assumption when you use regex to process HTML.
You can just remove the ^ in your current code, since it matches the start of the string.
EDIT
Number of spaces should be at least 1, so your should change the * after \s into +
I'm not a big fan of regex, so I'd do this:
var temp = document.createElement('div');
temp.innerHTML = '<script type="text/html"></script>';
var type = temp.childNodes[0].getAttribute('type');
if (type == 'text/javascript') {
// ...
}
If you were using jQuery, it would be way easier:
if ($('<script type="text/html"></script>').prop('type') == 'text/javascript') {
// ...
}
To account for the 'type' appearing anywhere within the script tag, and multiple versions of quotes, use:
/<script.*?type\s*=\s*.text\/javascript./i
You could tighten it up by specifying all quote alternatives instead of '.'.
Trying to get the aspx name from the current page from javascript?
Try this
<script language="javascript">
var url = window.location.pathname;
var myPageName = url.substring(url.lastIndexOf('/') + 1);
alert(myPageName);
</script>
As an alternative -
var page = '<%=Path.GetFileName(Request.Path)%>'
Do you want to parse the current window, or rely on asp.net?
Both work - it's up to you.
To Get The Exact Formname using jquery one can do string manipulation as shown.
$(document).ready(function () {
var path = window.location.pathname;
var page = path.substr( path.lastIndexOf("/") + 1 ).split(".",1);
var formname=page[0].toString();
alert(formname);
});
This one without the query string
$(function () {
var url = window.location.pathname;
var pageQuery = url.substring(url.lastIndexOf('/') + 1);
var page = pageQuery.split('?')[0];
)}
I want to attach custom ids to links based on query string and I found this very helpful post by Gregory (please take a look at it to get some context).
What I am trying to achieve is, if I go to www.mydomain.com, i require a default value to be added to the link on the webpage, example: www.ramdomdomain.com?myID1=default_value
I guess something must be edited after the if(hrefvalue==null) line in the code, but I can't figure out how to do it. Please help me. The following is the code I'm using:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function getHrefValue(key,url){
var query=new RegExp("[\\?&]"+(key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"))+"=([^&#]*)").exec(url);
return (query==null)?null:query[1];
}
function matchHrefs(){
var config={
'myid1':["#topnav a[href^='http://www.domain.com']"],
'myid2':["#topnav a[href^='http://www.domain.com']"]
}
for(var current in config){
var myvalue=getHrefValue(current,location.search);
if(myvalue!=null&&myvalue!=""){
$(config[current].join(',')).each(function(){
var href=$(this).attr('href');
var hrefvalue=getHrefValue(current,href);
if(hrefvalue==null){
var href_split=href.split('#');
$(this).attr('href',href_split[0]+(href_split[0].indexOf('?')>-1?'&':'?')+current+'='+myvalue+(typeof(href_split[1])!="undefined"?'#'+href_split[1]:''));
$(this).addClass('selected selected-'+current);
}
if(hrefvalue==""){
$(this).attr('href',href.replace(current+'=',current+'='+myvalue));
$(this).addClass('selected selected-'+current);
}
});
}
}
}
$(function(){matchHrefs();});
</script>
<div id="topnav">Random domain</div>
Is this what you're after:
<div>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>
<script type="text/javascript">
$(function() {
var requestid = new String(gup('myid'));
if (requestid == "") {
requestid = "unknown";
}
$("a").each(function() {
var href = $(this).attr("href");
href += "?myId=" + requestid;
$(this).attr("href", href);
})
})
//gup taken from here:http://www.netlobo.com/url_query_string_javascript.html
function gup(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
</script>
<div id="topnav">
Random domain
Random domain
</div>
</div>