Externalize Javascript in YAP - javascript

I am working on a Yahoo! App which requires certain external Javascript Frameworks to be loaded and used. Also in the Yahoo! App Best Practices Guide, it is also mentioned that the sources should be externalized, however, externalization isn't working for me.
I am using the standard procedure to load the external JS file like the following:
<script src="http://www.google.com/js/nxsl.1.js"></script>
But the above statement is giving me an error that external sources are not allowed.
Is there any way to use external JS files as I don't want to include all of my JS Login inline, it doesn't make sense to me and majorly my code won't be re-usable.
Any thoughts ?

Take a look at Yahoo's Get utility. It's part of their YUI library. It enables cross-site loading and is easy to use. You can read about it here:
http://developer.yahoo.com/yui/get/

Related

Add js external libs to embedded Camunda with Graalvm js-engine

I'm trying to implement some complex business validation logic via JS in DMN using java embedded camunda with spring boot. To avoid usage of deprecated Nashorn and add ES6 support, I've added to project graal js engine. It seems it works pretty well, but I have no idea how to add custom functions into context of js-script engine. Let's say I have index.js in my resource folder with exported functions, how can I register such extension to be able to use it into camunda?
Graal.js already works to a certain extent, but official support for it is only added in 7.16. Please see: https://jira.camunda.com/browse/CAM-13516
Specifically the point of allowing to load external scripts by default is in discussion there. You may explain your use case in a comment on the ticket.

Including external JS script without the HTML script tag

Pardon my ignorance, as my knowledge of JS is very shallow.
I'm currently using UnrealJS to connect to a Google Javascript API. UnrealJS basically runs a V8 instance inside the Unreal 4 engine. Considering that there's no conception of a DOM or HTML really inside UnrealJS, I need to be able to link to the external Google JS API script without the script tag:
<script src="some-external-script-url" />
Is there a way to do this? UnrealJS also supports Node.js - so if there's a way to do it with native Node.js, that would work as well. I'v looked into using require() from Node, but it seems to only be available for local scripts.
Apologies if this question is too vague or hard to understand, I will edit it if need be.
I'm not familiar with UnrealJS, but you've mentioned you can run Node code, and require modules. If that's the case, you can use an HTTP/S request to get the remote code, and then use vm.runInThisContext to inject the script into the current context. A sample code can be found in this answer.

How do I include a JS library without downloading it, if library do not have a CDN?

I am trying to write a web scraping code that does not need any prior setup.
When I send the code to someone, I want him to be able to copy paste the code and use it right away, without having to manually download any libraries etc.
For jquery and bootstrap, I achieve this by simply using CDN's. But I couldn't find any CDN's for jsoup or htmlunit.
Can I somehow make my code include those libraries automatically, without depending on a directory of the computer it is run on?
jquery and bootstrap are javascript libraries, primarily used for building web pages and such.
jsoup and htmlunit are java libraries, these can be used to build standalone applications.
You are asking about two completely different things here.

Best way to use JavaScript in a HTML page

What is the best way to import and use JavaScript code in HTML pages? I am considering two options:
Have single <script> tag with all.js file that is imported to HTML files, that uses selected JavaScript methods.
Define separate <script> tag in each HTML file separately.
What is the best design option here?
Lukasz, this innocent question is a big topic. To get started with JavaScript performance and maintainability in a nice place, follow Steve Souders' writings on this.
And use JSHint. That's much easier if your code is in a JavaScript-only file. To find out about other good practices, read idiomatic.js.
A single minified file is the most performant - less requests and smaller download. But not one global js for everywhere, that would be a pain to debug/develop.
Use a cdn for libraries as probably cached in browser.
IMO I will have separate tags for separate javascript snippets/plugins, it will make my javascript code easier to maintain. I can remove the plugin/add the plugin without disturbing the other javascript snippets.
But for performance issue, modern browsers will run 6 consecutive threads to download the resources, so it will be good idea to profile the loading times of the site/web app. You can use tools like Firebug/yslow to profile http requests being sent from the html page.
Personally I use template engines like Sitemesh and create templates for jsps for different parts of the application.
And for javascript I mainly use JQuery and a lot of plugins.
Minify js is a good and acceptable way. it also helps to build js files on the server side at run time. This is a big topic. Here are some good advices from yahoo. I use these as my guidlines. Also read on the topic js minify
http://developer.yahoo.com/performance/rules.html

Alfresco debugger cannot open js including an import tag

I'm implementing a custom document-details action in Share on community 4.0.a.
This action is using repository webscript that acts as an HTTP POST handler.
So I went to /alfresco/service/api/javascript/debugger to enable the js debugger tool.
But when I try to open a js file manually like aspects.post.json.js it launches a syntax error.
it does it with any js that starts with:
<import resource="classpath:/alfresco/xxx/xxx.js">
Is there anything I should be aware of to use this debugger with such files?
The "import tag" is not valid javascript. Thats why javascript syntax aware editors complain. As Florian mentions, it is resolved before the "whole" javascript is fed to the interpreter (rhino).
Nevertheless, would be nice if Alfresco would replace this tag with valid javascript to make tools happy - maybe with something similiar to the require function node.js provides.
I have opened an improvement request (or rather contribution) in the Alfresco JIRA which addresses this issue by providing a clean import API in JavaScript. In case you want to play around with it, you can grab the patch files as attachments at https://issues.alfresco.com/jira/browse/ALF-13631
With this, your example
<import resource="classpath:/alfresco/xxx/xxx.js">
becomes
importScript("legacy", "classpath:/alfresco/xxx/xxx.js", true); //Repository tier
importScript("classpath:/alfresco/xxx/xxx.js", true); //Share tier
I have never tried it but I am pretty sure that the debugger can't handle the statements. The debugger comes from the Rhino javascript engine and the import tags are an extension from Alfresco. They are resolved before the script is actually run in the Javascript engine.
If possible, try to separate the actual javascript code into different .js files as described here: Alfresco Web Scripts using Javascript – Part 1.
The first challenge when structuring your Web script code is how
Alfresco imports additional Javascript files. Alfresco expects
xml-style tags at the top of the main Web
script file. This will break javascript validation, automatic code
indentation and other important editor features.
This can be solved by placing all Javascript code in separate files,
leaving only the import declarations in the main Web script .js file.
Of course, this only works if you got full control over the webscript files..

Categories