Create JavaScript with Umbraco - javascript

Do you know if there is a way to create a javascript or PHP file in Umbraco?
By this I mean that I would like to have the user populate fields in the same was as creating a page, but for it to output as .js or .php rather than the usual .aspx
Many thanks!

I don't see any reason why you couldn't; you could use a controller to output as pretty much anything by setting the mime-type.
I'd be wondering though what your use-case for generating PHP files would be though; the server would still need to process the PHP script somehow; which may not be a trivial task (unless you just want to generate the PHP file for download/display the code) as it isn't client-side script at all.
JavaScript on the other hand is entirely a different matter - as it's processed client-side you could just point the browser at a url that happens to deliver the generated JavaScript. You could even render the JavaScript out from a WebApi end-point and the browser client would quite happily consume it.

Related

How to I turn a python script into a web app?

Sorry for the noob question, but I created a script that web scrapes data off our sccm server and compares it to programs that need to be pushed to hostnames.
My script works perfectly when I run it in pycharm, but I would now like to make this program available to the other guys on my team via webpage, instead of them needing to borrow my computer everytime, LOL!
Is there a way I can use a javascript front end to let them enter their username, password and serial number, and than based off that info run my script?
I'm not a programmer to be honest so I apologize if this seems like a straightforward thing.
This would be difficult for someone to just 'answer' but maybe a rough outline could help?
I'm certainly no professional but this is the way I might approach the issue.
create HTML/CSS/JS front end page with a form
upon clicking the 'submit' button have the JS take form data and export it into a JSON (just did a quick search on 'execute python script on button click' and things like that to find some implementations of the idea)
pass the JSON from the page onto the server and have the python script open up the JSON for the arguments it needs to use
python 'wraps' the results up into a JSON file and gives it back to the front end where you have another JS script that unpacks the JSON file and gives it to the user requesting the info. Alternatively, you could have Python put the data into some sort of excel sheet or csv or whatever and have python email the results to the user.
Once all done you'd simply have an instance of Python and all the appropriate libraries sitting on the server ready to use. Pycharm is simply an editor for code. You could theoretically install python itself, and the appropriate libraries needed for your code and just give your folks the .py file itself and have them run the script out of CMD/Terminal and achieve the same result.
Like I say, no professional by any means, and I'm sure I skipped a couple steps but that's a general outline. Obviously in the forms you'd wanna have validation and such to make sure the appropriate data gets used to avoid errors.

uglifyjs javascript inside php files

My project has a lot of JS code inside PHP files. I want to minify the JS code in these files and I like the uglifyjs2 program. Is it possible for that (or any) JS minifier to act on JS code INSIDE a php file?
Someone is going to suggest removing all JS code from the PHP files and placing it in .js files - but that isn't always practical.
Keep in mind that I do NOT wish to minify the JS on the fly (acting on the output of the PHP interpreter). I am delivering PHP code to customers containing JS, and I wish to minify the JS inside the PHP files that I am delivering.
What you're looking for does not exist.
The only real reason to have inline JavaScript inside your PHP files is because you need to interpolate PHP and JavaScript in order to dynamically generate some or all of the script. You cannot reliably minify such JavaScript before the PHP is actually processed, so you'd be looking at minifying it after the PHP is run, on every request. There should be very little value to this, as the vast bulk of your JavaScript should not be written inside your PHP files.
Whatever the php script generates as output is sent to the browser. If you sent the "actual" output of the script instead of the output stream to another application and fetch and redirect that output - yes, you can do just anything.
Another question is how feasible and reasonable that approach is. uglifyjs2 is a node.js appliation, and therefore this step would require some kind of interprocess communication, i.e. extra-complexity and extra time/memory/points-of-failure and all that good stuff.
Maybe it's worth the effort, maybe not. Maybe something like https://github.com/tedious/JShrink will suffice...

hiding javascript code or get it outside root folder?

I am developing an application and have some important information inside the code like IPs and stuff which has to be private. I use apache web server as a server and wanna know if there is any way hide a javascript code or moving it outside root folder ?
No, by definition. Javascript code is run on the client's computer. That means that the client must have access to the Javascript source for it to work. You could use AJAX to hide certain data until it is needed, but even then the client (and any user with Firebug) would be able to view it.
The only way to "hide" it would be through some sort of obfuscation utility or to minify the javascript. But, unfortunately, once the javascript code makes it to the browser. There is not much you can do about it.

How to parse JSON/XML file into javascript?

I made a program in Java which takes an XML document, which user chooses, and present its content on screen (on JLabel using Gui). That works fine. Now, I need to create it as a web based. I want to parse a JSON file, instead of xml file, into JavaScipt. I tried to use JSP and JavaScipt in combination with HTML5 but I didn't really find the correct way to do it.
So I am wondering if there is a possible way to do it this way or if it is better to use servlets (send the data in server side).
Also, I am wondering if it is better to use the existing Java code I have, using Javabeans, instead of JavaScipt. And then combine servlets with JSP in order to print the result on screen. I know JavaScipt is quicker than Java in web applications but I think I can't see a way to make a connection between JavaScript and HTML5 to accomplish this parsing.

What is the "acceptable" upper limit for JavaScript array size?

I left this question as generic as possible, but I do have a specific problem that I need to solve in my application and the answer to this would help.
The application I am working on uses PHP/MySQL as its backend, and is set up so that no text/words/phrases visible to the user are hardcoded in the HTML/JS that is output to the browser, rather they are stored in a database table associated with a language key that is used to fetch the correct translation of the word/phrase based on the user's language preference. Now this works great for text that exists inside the application's HTML, but in order for this system to work with the javascript files, all javascript must be placed in a .php file and wrapped in <script></script> tags and included inline with the HTML, CSS ect.
This creates some problems with the flexibility in the system's javascript, as it cannot be included as external scripts via <link> tags (I guess unless you set the .php file's headers manually), and perhaps more importantly it cannot be minified/packed etc. when served in the production environment.
My first thought of a solution to this problem is to have a php script that's placed before any other javascript which loops through every record in the language database table and creates an associative javascript array using the language key as the array keys and setting their value to the translated phrase according to the user's preference. So, in this way all javascript files could be made into actual .js files and link'ed, minified, packed, etc. as needed, and they would just reference the phrases they need from the master language array that was created (i.e. alert(LANGUAGE.some_text);)
Only problem is, the number of elements in this array could easily get into the thousands possibly even bigger. So back to my original question, what is an acceptable max size for a javascript array, based on the average PC? Or am I attacking this problem entirely wrong to begin with?
I think the problem has less to do with how much data javascript can theoretically handle and more to do with how your application is handling the data.
It sounds like you're returning all of the phrases for the user's language on every page, not just the phrases they need on that particular page. If that's the case, fixing it will be part of the solution to your problem.
The rest of the solution will be not using javascript for anything until the app is completely functional. Then go back and do progressive enhancement stuff with js.
Instead of generating javascript from those database queries, generate pages (server-side) in the user's natural language, and serve them from a separate subdomains/subdirectories. Have your web server load the appropriate config for the user's language based on subdomain/subdirectory.
It's not the answer you were looking for, but I hope it helps.
JavaScript is by its nature a scripting language. The interpreter is nestled inside the browser's kernel. Correct me if I'm wrong, but I don't believe there is a definitive upper limit. The only thing that constricts an unlimited upper bound are memory constraints.
You can house gigs of uncompressed data (more if you compress it).
You're more likely to get one of the top errors in the list here, before you'll see any "upper bound limit reached."
in order for this system to work with the javascript files, all javascript must be placed in a .php file and wrapped in tags and included inline with the HTML, CSS ect.
This creates some problems with the flexibility in the system's javascript, as it cannot be included as external scripts via tags
Actually, not necessarily true. You can serve javascript files (not html) using PHP. Of course doing this most likely mean that all your javascript file will have the extension .php but that is a small matter. If you don't care about being strictly correct you don't even have to set the Content-type to js since browsers will treat anything served by <script> tags as javascript anyway.
A lot of sites actually do this, though not necessarily in PHP. Google, Yahoo etc often serve javascript using a server side scripting language to enable them to do any or all of the following:
automatically concatenate javascript/css files into a single file
automatically minify the javascript/css files
automatically obfuscate the javascript files
automatically do dependency resolution to source needed javascript files
Some people use mod_rewrite to rename the .php (or .pl or .cgi) files to .js to hide the details of the implementation. But it's strictly not necessary.
Here's an example bookmarklet that I serve as a .php file.
I tried solving a similar problem as a non-web programmer before, and ended up hosting the language package as a separate XML which JS queries into. Bad idea, and I'll tell you why. Google can't see pages filled dynamically with JS. But if that's no issue to you, I would recommend that way, simply because I don't know any other. :)
The method Array.prototype.push , append arguments to the end of the array, and return the new length.
In some JavaScript engine such as v8, the length is bounded to 32bit unsigned integer, so this might be the limit you want to know.

Categories

Resources