translating from javascript to jquery - javascript

i'm trying to translate code from javascript to jquery and i need a little help
so , i have this code:
$('input[type=file]#upload_input_general_uploader').change(function(e){
var browserName=navigator.appName;
if (browserName=="Microsoft Internet Explorer"){
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
}
}
and i tried to translate it to jquery , that's what i got but that seems to not work
$('input[type=file]#upload_input_general_uploader').change(function(e){
$browserName=navigator.appName;
if ($browserName=="Microsoft Internet Explorer"){
$myFSO = new ActiveXObject("Scripting.FileSystemObject");
$filepath = document.$(this).val();
$thefile = $myFSO.getFile($filepath);
$filesize = thefile.size;
}
}
e.g for those who asked why - i need this code to be with jquery to be dynamic , i need to work with $(this) because this function get active for few inputs.
so , what's the problem here?

Start with this line:
document.$(this).val();
$ is not a property of document, so you'll get an error like TypeError: undefined_method when that line is executed.

How about:
$('input[type=file]#upload_input_general_uploader').change(function(e){
if ($.browser.msie){
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = $(this).val();
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
}
}
You don't really need to translate it if you don't need to. It's all javascript.

Related

File is not defined in JavaScript

I'm writing a code in JavaScript the opens a file then read it:
var txtFile = "/dataset/happy.txt";
var fileH = new File(txtFile);
var fileS = new File("/dataset/sad.txt");
var fileA = new File("/dataset/angry.txt");
var fileSC = new File("/dataset/scared.txt");
var fileSU = new File("/dataset/suprised.txt");
fileH.open("r"); // open file with read access
But I get (ReferenceError: File is not defined) error
What's the problem?
Thank you :)

Save Excel file via web page using java script

var myApp = new ActiveXObject("Excel.Application");
myApp.visible = true;
var myWorkbook;
var xlCellTypeLastCell = 11;
myObject = new ActiveXObject("Scripting.FileSystemObject");
if(myObject.FileExists("xyz.xlsx")){
myWorkbook = myApp.Workbooks.Open("xyz.xlsx");
}
else{
myWorkbook=myApp.Workbooks.Add();
}
var myWorksheet = myWorkbook.Worksheets(1);
myWorksheet.Activate;
objRange = myWorksheet.UsedRange;
objRange.SpecialCells(xlCellTypeLastCell).Activate ;
newRow = myApp.ActiveCell.Row + 1;
strNewCell = "A" + newRow;
myApp.Range(strNewCell).Activate;
myWorksheet.Cells(newRow,1).value=document.getElementById("table1").rows[1].cells.item(0).innerHTML; myWorksheet.Cells(newRow,2).value=document.getElementById("table1").rows[1].cells.item(4).innerHTML;
myWorksheet.Cells(newRow,3).value=document.getElementById("table1").rows[1].cells.item(5).innerHTML;
myWorkbook.SaveAs("xyz.xlsx");
myApp.Quit();
The above code saves the excel for the first time, in the second loop it pops the window asking to save manually, i am trying to save the file automatically without manual intervention.
You need to switch off display alerts temporarily. This is in VBA. Change it to use myApp
Application.DisplayAlerts = False
' save here
Application.DisplayAlerts = True
The DisplayAlerts documentation is short and worth reading: https://msdn.microsoft.com/en-us/library/office/ff839782.aspx

Use JScript to insert Price List on Quote onOpen in Microsoft Dynamics CRM 2011

Here is my code, I get an object error onLoad. Please help.
function Setlook()
{
var lookup = new Object();
var lookupValue = new Array();
lookup.id = "7b31D4D998-F124-E111-96C3-1CC1DEEA";
lookup.entityType = 1022;
lookup.name = "Default";
lookupValue[0] = lookup;
Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupValue);
}
The code itself looks correct, but the GUID of the lookup doesn't. It doesn't have the right format nor does it have the right number of characters (32). Fixing that should eliminate the error.
Here is the proper syntax, the important thing is to have the correct .typename
function Setlook()
{
var value = new Array();
value[0] = new Object();
value[0].id = '{31D4D998-F124-E111-96C3-1CC1DEE8EC2D}';
value[0].name = 'Default';
value[0].typename = 'pricelevel';
Xrm.Page.getAttribute("pricelevelid").setValue(value);
}

Writing json string into file using Windows Script Host

I am trying to write json string that I stringified using https://github.com/douglascrockford/JSON-js/blob/master/json2.js ( JSON.stringify) ,
I am trying to save the string and I am getting an error:
Microsoft JScript Runtime Error: Invalid procedure call or argument
at
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse);
this is my code :
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref);
WSH.echo(textPref)
// Create the new file.
fso.CreateTextFile("d:\\Preferences_temp", true);
var fileObj = fso.GetFile("d:\\Preferences_temp");
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateFalse); <- Microsoft JScript Runtime Error: Invalid procedure call or argument
textStream.Write(textPref);
textStream.Close();
The constants you pass to the FSO methods are not exposed via late binding with new ActiveXObject so you need to define them independently;
var ForWriting = 2;
var TristateFalse = 0;
Edit
var ForWriting = 2;
var TristateTrue = -1;
var textPref = '{"xxx": "AA \u05D5 BB"}';
var pref = JSON.parse(textPref);
textPref = JSON.stringify(pref)
// Create the new file.
var fso = new ActiveXObject("Scripting.FileSystemObject");
//create as unicode
fso.CreateTextFile("c:\\null\\Preferences_temp", true, true);
var fileObj = fso.GetFile("c:\\null\\Preferences_temp");
//open for unicode
var textStream = fileObj.OpenAsTextStream(ForWriting, TristateTrue);
textStream.Write(textPref);
textStream.Close();

Jscript ReadLine() related

Can any one please tell me that we use ReadLine() to read a particular line from a file (.txt). Now I want to read the total content of the file (not only the first line). For that what method I need to use. I googled a lot but I cant get the solution.
My Code is given below:
var ForReading = 1;
var TristateUseDefault = -2;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFile = fso.OpenTextFile(sFileName, ForReading, true, TristateUseDefault);
var importTXT = newFile.ReadLine();
This is returning the first line of the .txt file by importTXT variable. Now I want to get the total file content in importTXT.
Any suggestion will be very much helpful for me.
You use the ReadAll method:
var importTXT = newFile.ReadAll();
(Don't forget to close the stream when you are done with it.)
Here: ReadAll (msdn)
I found the example given very poor - for example it did not CLOSE the file, so I added this to the msdn page:
function ReadAllTextFile(filename)
{
var ForReading = 1;
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Open the file for input.
var f = fso.OpenTextFile(filename, ForReading);
// Read from the file.
var text = (f.AtEndOfStream)?"":f.ReadAll(); // this is where it is read
f.Close();
return text;
}
var importTXT = ReadAllTextFile(sFileName);

Categories

Resources