I'm using JavaScript in HTML to create an HREF link from a couple of variables.
The first variable is the fixed text for the start of an Android file path (the same every time) and the second variable is the file name which is taken from an XML attribute.
It's all works nicely, BUT it only constructs the link properly if I DO NOT have any spaces in the file name variable from the XML document.
Basically what's happening is if the file name variable contains a space, it only constructs the link up until the first space in the file name so an example would be
**Correct link =**
example file name.pdf
**Link my code incorrectly returns =**
example file name.pdf
Help would be greatly appreciated.
Thanks!
<script>
xmlDoc=loadXMLDoc("PBFileNames.xml");
x=xmlDoc.getElementsByTagName("file");
var path = "file:///sdcard/Clients/PB/"; //this will be constant between all iterations
for (i=0;i<x.length;i++)
{
var filename = x[i].getAttributeNode("name").nodeValue; //the nodefile is the filename
{
document.write("<br>");
document.write("" + filename + "");
document.write("<br>");
}
}
</script>
You have to escape the URI
document.write("" + filename + "");
Related
I have a c# function that builds a string which in turn is used as a hyperlink to another page. However, with some strings with single quotes it is causing a javascript error as shown here:
I'm calling the javascript function in the code behind as so
linkFullMatch.NavigateUrl = "javascript:showFullMatches(" + sb.ToString() + ")";
the javascript is on the aspx function as so:
<script>
function showFullMatches(url) {
window.open(url, "_blank", "height=344,width=1100,scrollbars=yes,resizable=yes,toolbar=no,location=no,status=no,menubar=no,left=580,top=194");
}
Any help would be greatly appreciated. Any string that doesn't have a single quote in works fine and the page link opens as requested.
Rob
You need to add an additional layer of quote marks to make the sb.ToString() value an JS string. Adjust your call like:
linkFullMatch.NavigateUrl = "javascript:showFullMatches('" + sb.ToString() + "')";
Note the additional ' marks.
I'm using JavaScript to parse a XML file. One of the XML's attribute is an URL which links to an image. eg. http://localhost/pic.jpg
As the parsing goes on, I use an variable to hold each URL(which is a string variable) and hoping that I can show these images in a table.
The following is part of my code:
for(i = 0; i < row.length; i++)
{
// looping through the XML file
var Logo1 = row[i].getElementsByTagName("Logo")[0].childNodes[0].nodeValue;
//Logo1 is the string which is a URL
document.write("<img src= 'Logo1' width='256' height='128'>");
}
I use Chrome, but the images won't load....
enter image description here
Can someone help?
If Logo1 is a variable containing an image path (URL, as you say), you need to build the img tag in a way that includes the path:
document.write('<img src="' + Logo1 + '" width="256" height="128">');
This concatenates the Logo1 string variable into the rest of the tag.
That said, document.write is probably not going to work for you in the long run. you would instead create a new Image object and set its src property:
var testImage = new Image();
testImage.src = Logo1;
// add testImage to the DOM
I have a series of HTML files in the same directory named: page1.html, page2.html, page3.html, etc. I can identify the name of my current HTML document like this:
var path = window.location.pathname;
var page = path.split("/").pop();
console.log( page );
But now I want to be able to log the next HTML file in the series. So if I am on page2.html, I want to log page3.html. How can I replace that file name, with the next numbered file in my series ?
This should do it with pure javaScript:
var page = window.location.pathname;
console.log(page.replace(/\d+/g, parseInt(page.match(/\d+/g), 10) + 1));
Inner regex takes page number + 1 and the outer one replaces the value with it.
That's quite simple:
page=str.replace("page","");
a=parseInt(page);
a++;
console.log("page"+a+".html");
The first line raplaces the first part so it's "3.html" var. The second gets the integer out of page. What follows is an increment and an output.
That's one of the possibilities how it could be done.
Sources:
str.replace()
parseInt()
Thanks to #Mico and #SugarOnBacon this solution works!
var path = window.location.pathname;
var page = path.split("/").pop();
console.log(page.replace(/\d+/g, parseInt(page.match(/\d+/g), 10) + 1));
I am getting the file name which includes the type from an html input,
var file = jQuery('#attachment')[0].files[0];
var fileType = file.name.substr(file.name.indexOf(".") + 1);
and this works great if the file name is FILE.TYPE but if the file name if FIlE.Name.TYPE it does not work correctly.
How can I make sure I get the last dot before the file type? Or is there a better way to go about getting the file type?
indexOf always goes from left to right and get the first match. Fortunately, there is the opposite method which goes from right to left. That method is called .lastIndexOf().
var fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
Try this one:
var fileType = file.name.split(".").reverse()[0];
This will work for any number of . characters
If you are on http://www.cnn.com/2012/11/09/opinion/brown-pakistan-malala/index.html can you get Jquery to grab the index.html?
or if you are on http://www.washingtonpost.com/politics/decision2012/supreme-court-to-review-key-section-of-voting-rights-act/2012/11/09/dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html have it return dd249cd0-216d-11e2-8448-81b1ce7d6978_story.html?
And for non extension defined pages such as this current one http://stackoverflow.com/questions/13317276/jquery-to-get-the-name-of-the-current-html-file can it return the last "file" in the "directory"structure, for example: jquery-to-get-the-name-of-the-current-html-file
Although not JQuery, you can access it using the following:
document.location.href.match(/[^\/]+$/)[0]
or
document.location.pathname.match(/[^\/]+$/)[0] in case of unneeded anchors/hash tags (#).
location.pathname.split('/').slice(-1)[0]
No need for jQuery. This will give you the last segment of the URL path (the bit after the last slash):
var href = document.location.href;
var lastPathSegment = href.substr(href.lastIndexOf('/') + 1);
function getCurentFileName(){
var pagePathName= window.location.pathname;
return pagePathName.substring(pagePathName.lastIndexOf("/") + 1);
}