I have a JavaScript program that worked until I tried to change this: "foldername" to this: http://hokuco.com/test/"+"foldername"+"/index.html".
what is wrong with my code?
For anyone interested entire JS:
document.getElementById("submit").addEventListener("click", function(){
var url = document.getElementById("http://hokuco.com/test/"+"foldername"+"/index.html").value;
window.location.href = "url";
});
<input type id="foldername"></input>
<input type ="button" id ="submit/>
You probably meant:
document.getElementById("submit").addEventListener("click", function(){
var url = "http://hokuco.com/test/" + document.getElementById("foldername").value + "/index.html";
window.location.href = url;
});
Changes:
The parameter in the getElementById function is the same as the id attribute on the input element with the id "foldername".
The window.location.href should be set to a variable, not a quoted string.
More legibly, you would want:
document.getElementById("submit").addEventListener("click", function(){
var folder = document.getElementById("foldername").value;
var url = "http://hokuco.com/test/" + folder + "/index.html";
window.location.href = url;
});
Now, hopefully, it is much more clear about what's going on.
Please read the following documentation to better understand document.getElementById: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
Your code will most likely return an uncaught type error when trying to access a property of null. You must only pass a string referring to an Element object's ID.
What are you trying to accomplish? It looks like you are trying to redirect but are using a string literal instead of a variable.
Related
I create a function using Javascript for related post on my Blogger template,
Here is my code:
function toHttps(link) {
var protocol=link.replace(/\:/g,'');
if(protocol=='http') {
var url=link.replace('http','https');
return link.replace(url);
}
}
if my original url is
https://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html
Why is the result like this?
https://dpawoncatering.blogspot.com/2008/08/undefined?
Naren Murali's answer is correct. I'd just like to add a different way of doing "protocol" swap using javascript's own URL parser that might be interesting for other people.
You can instantiate an a element and use its href attribute to parse your URL, then you can access and change the protocol attribute of the href and retrieve the resulting URL:
function toHttps(link) {
var url = document.createElement('a');
url.href = link;
url.protocol = 'https';
return url.href;
}
Since the URL contains https already it does not enter the if condition, hence nothing is returned hence we get undefined, please check my corrected function. Let me know if you have any issues!
function toHttps(link) {
if(link.indexOf('http://') > -1){
var url=link.replace('http','https');
return url;
}
return link
}
console.log(toHttps('http://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html'))
console.log(toHttps('https://dpawoncatering.blogspot.com/2008/08/nasi-box-murah.html'))
The code looks as following:
<script type="text/javascript">
function Hello() {
var name = DevExpressUtility.getSelectedItem();
var url = '#Url.Action(' + name + ', "Home")';
//alert(name);
//window.open("../../Views/Home/DisplaySchema9.cshtml", "_blank", "titlebar=no,location=no,toolbar=no,scrollbars=no,resizable=no,fullscreen=yes");
window.open(url, "_blank", "titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,fullscreen=yes");
}
</script>
There is error message - Unrecognized escape sequence.
Not really.
Looks like you have name that is computed client-side but then you believe the server will pick up the current value and somehow re-evaluate the url every time something changes at the client?
It won't work that way.
One of workarounds would be to not to rely on the #Url.Action but consider your current routing and hard-code the actual path. Assuming you have default routes, it should be as simple as
var url = '/Home/' + name;
Am trying to create a link to create an email to send information to the user, the body of which needs to be filled with data generated by a javascript function, and hope that someone can help.
Idealy, if I could substitute the 'body_blurb' below, with a string returned from a javascript function called at the time of clicking that'd be perfect.
e-mail data
Appreciate your time
I just assigned an id to the link here, but you could create something more generic if you wanted. Once you have an onclick handler created you can access the url with href. Set this to whatever you want.
http://jsfiddle.net/f3ZZL/1
var link = document.getElementById('email');
link.onclick = function() {
this.href = "mailto:you#yourdomain.com?subject=Data&body=";
this.href += getBody();
};
function getBody() {
return 'HelloWorld';
}
Here is the no-spam method
var link = document.getElementById('email');
link.onclick = function() {
var name = "you";
var domain = "yourdomain.com";
var linker = "mailto:" + name + '#' + domain + "?subject=Data&body=";
linker += getBody();
link.setAttribute("href", linker);
};
function getBody() {
return 'HelloWorld';
}
On Fiddle
You don't actually need to set the href value. What you're really trying to do is send the browser to a mailto: URL: the href is just a means to that end.
Leave the href blank and make the link have an onClick handler. In the onClick, feed the browser the mailto: URL (after you build it using your variable). Presto, done.
Im sorry if the title is seem confusing :(
I Using KC Finder to develop my CMS file manager
i have HTML code like this
<input type="text" name="url-1" value="" />
<button class="button-primary" onclick="openKCFinder_singleFile(); return false;">Insert</button>
here is the content of openKCFinder_singleFile()
function openKCFinder_singleFile() {
window.KCFinder = {};
window.KCFinder.callBack = function(url) {
window.KCFinder = null;
console.log(url) // URL is the call back result from KCfinder, example: /image/a.jpg
};
window.open('/wp-content/plugins/kcfinder/browse.php', 'kcfinder_single');
}
My Question is, how to set the value input with the value i get from kcfinder, as you can see we get the value from variable url, how to pass that to set as input value?
if the code in jQuery script, i really appreciate it :)
Many Thanks
GusDe...
Using jQuery, you can simply add :
$('#url-1').val(url);
See this.
I agree with the answer of user284291, but there is a but :) if you are using this function in a page which does not load jquery, you can send the url by replacing
$('#url-1').val(url);
with
var x = document.getelementbyid('url-1'); x.setAttribute('value', url);
I have code:
function _filter() {
var url = window.location;
alert(url);
alert(url.split("/")[1]);
}
When I launch it I get only one alert message:
http://localhost:8000/index/3/1.
Why I don't get the second alert message?
Adding .toString() works and avoids this error:
TypeError: url.split is not a function
function _filter() {
var url = window.location;
alert(url);
alert(url.toString().split("/")[2]);
}
When run on this very page, the output is:
stackoverflow.com
The location object is the cause of this, window.location is an object not a string it is the location.href or location.toString().
function _filter() {
var url = window.location.href; // or window.location.toString()
alert(url);
alert(url.split("/")[1]);
}
The value of window.location is not a string, you want the href property of the location object:
function _filter() {
var url = window.location.href;
alert(url);
alert(url.split("/")[1]);
}
Because your url is ans object so you need to convert this to string than you apply split function
function _filter() {
var url = window.location+ '';
alert(url);
alert(url.split("/")[2]);
}
The index [1] is in between the two slashes of http:// which is null and wont be alerted. Index [2] is the localhost:8000 you're probably looking for.
Simple window.location.hostname should be useful too.
To understand how many pieces you get from splitting operation you can alert the .lenght of url.split, are you sure that the script doesn't block?
Use firebug to understand that
url.split("/")[1] will equal to null. So, it alert(null) will not display msg.