I am new to javascript. I am trying to append a variable to a link like this. I am able to get the var name but it's not working when I append. i tried couple of other things. Any suggestions for appending the variable name.
<script type="text/javascript">
var name = document.getElementById('name').value;
window.location.href = "http://example.com?id=5678&name=+name+&code=1234
</script>
Simply end and restart the quotes like so
window.location.href = "http://example.com?id=5678&name="+name+"&code=1234";
You can imitate sprintf function by:
window.location.href =
"http://example.com?id=5678&name=%name&code=%code"
.replace('%name', 'myName')
.replace('%code', '123456');
Related
What I'm trying to do is to change the last character of a link using jQuery or Javascript.
The link that the client will input into the CMS looks something like this: https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0
What I'd like to do is to set the website to take this link and replace the '0' on the end of the link with a '1'.
Does anyone know how this could be done automatically?
Any help is appreciated,
Tom
As easy as that:
var url = document.getElementById("id of element").href;
url = url.substring(0, url.length-1);
url = url + "1";
document.getElementById("id of element").href = url;
Probably google first about string functions before asking...
If the pattern of the url stays the same (the parameter dl=0) then you can simply use the .replace()-function:
var url = 'https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0';
url = url.replace('dl=0', 'dl=1');
I want to get the my exact location (only PHP page) using javascript, I'm currently using window.location to get the exact url for example: localhost/empc/myfolder/functions.php what is the code if I want to get only functions.php as a result? Is it possible? Thank you.
You can use location object to do this, To remove the "/" before the file name you can use substring method.
location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
you can try this pretty simple
var url = 'localhost/empc/myfolder/functions.php';
var url = url.split('/');
alert(url.pop());
It is possible. Use the javascript split function to separate the string into pieces and then access the last element to get the file name:
var str = "localhost/empc/myfolder/functions.php";
var arr = str.split("/");
alert(arr[arr.length-1]); //this will alert functions.php
I have a file with this code:
var famname = guterriez;
And another HTML file family.html where I have included the location of the file in the head. I need to display the variable in a p element, and possibly in other locations on my site.
I would appreciate some help / tips on how to do this as I've done some research and nothing seems to be working.
If guterriez is not another variable but a string, then you have to put it into quotation marks. Like mentioned above.
Something like this might work for you, if you want to display the code in various locations on your page:
I.e. if you have a <div> or a <table> where you want to dispaly it:
<div id="MyDiv"></div>
<table id="MyTable"><tr><td id="MyCell"></td></tr>
The following JS Code will display the code in the mentioned above elements by calling them with their ID:
<script>
document.getElementById("MyDiv").innerHTML = famname;
document.getElementById("MyCell").innerHTML = famname;
</script>
Below is an example of how to integrate your JS file into your HTML, and reference variables defined there:
config.js
var txtVar = "This is some text";
page.html
<script src="config.js" />
<p id="textBlock"></p>
<script>
document.getElementById("textBlock").innerText = txtVar;
</script>
See code snippet below for a runnable example.
var txtVar = "This is some text";
window.onload = function() {
document.getElementById("textBlock").innerText = txtVar;
};
<p id="textBlock"></p>
I'm assuming guterriez is a string that you would like to assign to the variable famname. In that case, it needs to be enclosed in quotation marks to denote the fact that it is a string rather than a variable named guterriez (which presumably doesn't exist).
Try this:
var famname = "guterriez";
Then, you want to use innerText to assign this value to the p element in your HTML file. Try using document.getElementsByTagName("p")[index] to access the node and then assign innerText:
document.getElementsByTagName("p")[0].innerText = famname;
var famname = "guterriez";
document.getElementsByTagName("p")[0].innerText = famname;
<p></p>
Hi guys I'm just studying about the javascript and asp.net.
I want to insert/append a declared var in window.location.href, I have this code and its working just as I wanted to.
<script type = "text/javascript">
function myFunction() {
//var siteURL = document.getElementByID("txtURL");
window.location.href = "http://www.google.com";
}
</script>
This function is called/triggered through an 'onclick' event. But When I change the code to:
<script type = "text/javascript">
function myFunction() {
var siteURL = document.getElementByID("txtURL");
window.location.href = siteURL;
}
</script>
Can someone help me make the second code given work just like the first one. Thanks in advance.
First of all there is no document.getElementByID function. It's document.getElementById (last char differs).
This function will return object so to get it's value you should use innerHTML (or value if it's input field) attribute like this var siteURL = document.getElementByID("txtURL").innerHTML; and then pass this variable to window.location.href.
Your problems lay with getElementByID. Firstly, you've made a typo, the last letter should be lower case getElementById, and secondly, this returns a Node, from which you probably want it's value
var siteURL = document.getElementById("txtURL").value;
First of all, JavaScript is case sensitive language, so the correct method name for selecting an element by ID is document.getElementById().
Next, if element with ID "txtURL" is a form field, e.g. <input> or <select> you should get its value with siteURL.value. Otherwise if it is any other text container, e.g. <div> or <span> with URL as its text, you may use siteURL.innerHTML.
var siteURL = document.getElementById("txtURL");
//siteURL is just a reference to the text field, not it's value
your variable siteURL is just a dom object at this stage, you need to call the .value property to grab it's value to use as a href. and JavaScript is case sensitive so it's .getElementById() not .getElementByID(). The code below should work just fine
<script type = "text/javascript">
function myFunction() {
var siteURL = document.getElementById("txtURL").value; //add .value here
window.location.href = siteURL;
}
</script>
document.getElementById() returns a DOM object, not a string.
If you have your url stored in a node which ID is "txtURL", you have to access the information itself, probably if stored as contents of the node: via .innerHTML or via .html() in jQuery
document.getElementByID("txtURL") returns a DOM Node. window.location.href is a String-like object. Giving it a DOM Node is silly.
If #txtURL is an input, you can do:
window.location.href = document.getElementById("txtURL").value;
If it's a TextNode you can do something like:
window.location.href = document.getElementById("txtURL").nodeValue;
but this is getting iffy.
I am trying to insert some new links using ‘innerHTML’. As there may be a number of calls on the same ‘ids’ I thought it would be wise to use variables. The following does not respond beyond the alert? The process works fine if I don’t use ‘var link’ and just enter it in full. Is there an issue perhaps trying to do this with xhtml?
Thanks.
var newlink = '<a title="new link" href="newlink.htm">New Link</a>';
var link = "document.getElementById('idlink')";
if( link ) {
alert("link confirmed");
link.innerHTML = newlink;
}
var link = "document.getElementById('idlink')";
should be
var link = document.getElementById('idlink');
You are assigning a string to the variable. Just because the contents of the string looks like code that could be run doesn't mean that it actually runs. It's just a string.
Call the method and assign the result to the variable:
var link = document.getElementById('idlink');