Tinymce - Loading local file content to Tinymce Instance - javascript

I'm not sure how to go about this but I wanted to be able to load content from a file (i.e. txt, html, etc...) to my Tinymce instance.
So basically a button that initiates a file browser and then loads the content of the file to Tinymce.
I've seen this for images and image browsers but I'm not sure how to do this for files.
This would be done locally, not on a server as well.

Nick -
Prior to HTML 5, JavaScript in the browser cannot read files directly from the hard drive - this is done for security reasons:
https://en.wikipedia.org/wiki/JavaScript#Security
In HTML 5 there are new APIs that allow you to get to files. This page has a decent overview:
http://www.html5rocks.com/en/tutorials/file/dndfiles/
I would note that browser support for this is not yet universal:
http://caniuse.com/#feat=fileapi
Assuming you can use the File API you should be able to do what you want after a user selects a file.

Related

can't stop obsolete version of XML file from loading on web page

I have some generic javascript that loads and displays photos on an html page based on an xml file that it reads. The problem is that if I change the content of the xml file, it doesn't show up on the web page. Here's a section of an xml file to give you an idea of what I'm talking about:
<slides>
<slide>
<name>slideshow/cal2018/20180506.jpeg</name>
<alt>some alt text</alt>
...
The problem that triggered my issue is I decided to move the "slideshow/" into the xml file instead of having it in the javascript. When I did that, the photos stopped showing up - I just got the alt text. However when I switched to a different page, the photos were brought up properly.
The problem is that all the web browsers seem to want to hold on to xml files for a long time - apparently at least a week, which is how long I worked on the pages without the current version of the xml file being loaded.
I can manually clear the browser cache and the problem resolves itself for that browser. Simply reloading the page, which is sufficient to reload the javascript, doesn't do that.
I use a lot of xml files to drive web pages. I need to ensure that the xml file is the current one and not some ancient cached copy. Is there a cross-browser (javascript?) way I can clear the (xml) cache before loading an xml file?
Also, why do browsers ignore xml files when I ask for a page reload?

linking html web page to text editor to practice

I'm trying to link this page http://www.bauer.uh.edu/parks/f1471m.htm to my text editor ( text mate) with a mac.
I even copied the code and pasted it into an HTML file, and made a .js file (the one I'm trying to use to practice with) in the same folder and to use the local tag with no success.
I tried using src="http://www.bauer.uh.edu/parks/f1471m.htm" with no luck.
am I doing something wrong?
I'm making a few assumptions here...
From what you're describing, I think you're trying to directly edit the file. Unless you have write access on that server, what you're doing isn't going to work. Some web development software will allow you to do this, but most text editors don't.
Can you work on the file locally (on your computer's file system) and upload it to the server through FTP?
Many browsers give you the ability to save a page locally these days. That would set up the proper structure for you on your own machine. (FireFox can do this using Save Page)

Is it possible for a remote html file to download a file to a client?

I'm an iOS dev looking into some security aspects of a native iPhone app that loads local html pages into a UIWebView (that's the iOS class that give browsers functionality to a native app).
If the app loads a local html file and that file contains a link to an external html file then is it possible for that external file to download a file to the client (another html file or a javascript file)? My knowledge of html/javascript isn't detailed enough in this area.
E.g. If A.html is file physically present on the phone and A.html contains a href to B.html where B.html is on a server then is it possible for B.html (using whatever means) to move a file (C.html or D.js etc.) from the server onto the device?
What you probably mean is : are HTML pages able to download (and potentially execute or overwrite) content/code on the device? I'm just going to assume that in the following.
First, the browser isn't supposed to be able to download anything without the user consent. That's especially true on iOS, where the browser actually can't download anything at all. (Well, you can actually download images by long-pressing on them, but they are only going to go to the Photo Roll. Any other download will just fail.)
HTML pages (and JavaScript, images, ...) are of course downloaded to the device before the browser or UIWebView displays them, but they can't access anything on the filesystem.
What's more, due to the same-origin policy, a web page cannot access anything that's outside of its domain (cross-origin requests allow this, but they require the server to send a particular header).
Say your page is on http://mywebsite.com : you can't make a request to http://blah.org if it's not setup properly, or even (much more dangerous) to file:///etc/passwd (which you have no way to setup properly).
I'm not sure about local pages, but (at least on Chrome on the desktop) local web pages cannot download anything at all.
Of course, all of these are the expected behavior.
There have been cases of vulnerabilities (this one for instance) where Safari allows unprivileged access to the filesystem, but in most cases they have been patched quickly by Apple and you won't have to (and are not supposed to if you want to avoid unnecessary headaches) worry about them.

Current filetypes which are cached by current browsers

I am developing a website which is extensively using jquery dom javascript ajax etc. I am dividing the javascript files to be cached and it saves me around 0.2 seconds, also, I have templates on the main page, which are hidden and I switch between hidden and visible, when I want to use these elements on the page, (like div or table, some elements which are selected and inserted into another content div", but I want to load these static templates from a file, so that the browser gets the file, and on page refreshes, the browser laods the static html file from cache, and It will save me a lot of miliseconds this way. but I don't know if current browsers assume htm files are static. I am helping the browser to assume the file is static, by saving it as *.htm , instead of *.php. What file extensions should I use, for making the browser assume they are static files? would htm be assumed to be static? Would you suggest using *.txt?
You just need to set the correct HTTP caching headers on your files when you serve them.
https://developers.google.com/speed/docs/best-practices/caching
For static files you can configure your webserver to do this (eg Website image caching with Apache), for dynamic files you can do this on the fly (in PHP with the header() function).
Note that the file extension doesn't really matter either, you should tell the browser what the type of files is using the Content-Type header (though the extension will help your webserver guess this).

how to read a text file using Javascript

I am taking a text file from user and then posting that file back to the browser using ajax storing the content in db and then showing the content back to user page using Jquery post response.
Now i want to something like this..
Read the text file from the user computer using javascript. Display the content and when he submits the page I will save the values.
Can't be done in pure JS for security reasons. You would need to have the user upload the file to your server, and fetch the contents back through Ajax.
If you use Flash or Java, you should be able to gain direct access to the file. If you speak Flash/Actionsript, maybe SWFUpload's source code (especially the new client-side resizing functions) can serve as an inspiration.
Update: This blog entry should help. Read and write local files with Flash Player 10
Update: To elaborate on the "upload and fetch" thing, if you do the uploading in an IFRAME, you could even have the upload script simply output the text file's contents. Because the iframe belongs to your domain, you will be able to retrieve its contents via JavaScript when the upload has finished. As long as you send a content-type: application/text it should be fairly safe from any malicious attacks.
If you're ok with Firefox 3.6 support only check out https://developer.mozilla.org/en/Using_files_from_web_applications, otherwise you should use Flash, Java or silverlight for this.
You won't be able to read a file in user's computer due to security issues.
Reading client files in javascript is possible with the new File API available in modern browsers. Check this site and its code: http://www.readfileonline.com/
However, before reading file contents in javascript, the user must explicitly select the files it allows to read. This is a security feature of the standard.

Categories