I want to create a SignalR client in JavaScript. Am not using any HTML files(its not a web application).My intention is to create ".js" file and connect to the SignalR hub and subscribe to the events,and when the events got triggered I want to output the data to a console.
So how can I refer "signalR-2.1.0.min.js" in my plain .js file.
<script src="Scripts/signalR-2.1.0.min.js"></script>
The above way of reference works only with HTML files.This is working fine in my web application.
But if I want to run a ".js" file for the same purpose (as a SignalR client without html files)how can I add reference to this "signalR-2.1.0.min.js" file?
Thanks In Advance
Susmitha
If you your using Node.js, you can just use require:
const myFile = require('./myScript.js');
Related
I am trying to build a Chrome extension but I have encountered a problem. I need access to a file that is in the same directory as my JS script. Here is the representation:
my_script.js
My Folder
|-> my_file.file
I use a third party library that does some required things with it. And I need to pass in the path to my_file.file The problem is my_script.js gets injected into the website I am trying to change and the code instead of searching on the local file system it tries to find it in www.website.com/My Folder/my_file.file which obviously does not exist. How can I make it so that it seacrhes relative to my_script.js? I unfortunately can't pass the file itself to func(), it has to be the path of the file. The file I am trying to reach is about 200MB which I was planning on shipping with my extension. Also this project is vanilla JS. Thank you!
my_script.js
...
lib.func('My Folder/my_file.file').then(function (out) {
document.out = out;
});
...
I have an HTML file with JavaScript that I am running without any Webserver/host so I am just opening the file in a browser local to my windows PC. In that HTML file I would like to be able to read a text file in the same folder as the html file. That file will contain data in rows and columns separated with tabs. i.e
1 a
2 b
3 c
I want to keep this as simple as possible so all I have to do is share the HTML and Text file to others so the can open it up local to their computer without any webserver/host and without having to also copy of external libraries like node.js or jquery.
I have searched and tested everything I can find but either I need to reference an external library or I have to run it in a webserver or I need to click a button to load the file through the browser, none of what I want.
Does native JavaScript support the function to read a text file and save it to an array? If so, any code direction would be great.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
XMLHttpRequest() exists in native JavaScript, I think it will help you.
You also can send a request to the file. Or use library: axios.js because when you use XMLHttpRequest() you lose many time to write code which just get content from file, with axios I got file content with one line: `axios.get('file.txt').then(result => console.log(result.data));
To connect Axios: <script src="https://unpkg.com/axios#0.18.0/dist/axios.min.js"></script>
You can read official documentation about axios.js and XMLHttpRequest() in the net.
I am using clipboard.js to allow users to copy snippets of code from an API docs page.
<script> var clipboard = new Clipboard('.copyButton');</script>
When I run this script in my view the functionality all works as it should. However if i place the script in its own js file I get the error
Uncaught ReferenceError: Clipboard is not defined
I am using Yii2 framework so i am using assets to register sources like so:
public $js = [
"js/api.js",
];
And then I am registering that Asset inside of my main layout like so:
use metis\assets\ApiAsset;
ApiAsset::register($this);
Anyone got any ideas why?
It sounds like either the Clipboard object isn't being initialized in time, or the clipboard.js file is registered after the script that is using it. Can you verify from the client source that the clipboard.js file is defined before the script that is using it?
aspx file
string firsrName="jafer";
myscript.js
GetMyName();
function GetMyName() {
alert('<%=firstName%>');
}
I am not getting my value
The line alert('<%=firstName%>'); use the Web Form Page syntax. It is actually not possible to get the value like this because this syntax cannot be used in external JS files.
The simpliest (but not cleanest) method is to write the JS method into the layout file or another aspx file.
Read How to get asp.net client id at external javascript file
You could make a global variable in your aspx page and access it in your js using window.objectName
Inside HTTP adapter, I am getting JSON object back from REST service. I need to do some parsing of the reply before returning it back to the client.
I figured I can have another JavaScript file to do the parsing, so not to clatter my adapter.js too much.
What I don't know is how to make the 2nd java script file available in my adapter JS.
On client side I would use something like $.getScript("some.js");
JAVA classes can be accessed simply with classpath.className, but I can't find any references on how to load another Java Script file!
Please, help!
You cannot have additional JavaScript files referenced from adapter's JS.