Basically Im working with Sharepoint, help me god!
Sharepoint does some crazy the things but Im hoping what I want to do can be achieved using jQuery.
Sharepoint is writing some image values into a table:
<script>
fNewItem = false;
InsertItem("http://dev_site/sites/Pictures/Waves.jpg",
"5",
"BlueWaves",
"jpg",
"1920",
"1080",
"/_layouts/images/icjpg.gif", fNewItem);
</script>
There are a number of these output by Sharepoint, this snippet is from a Sharepoint Gallery so I'd need to loop through the page to find all of these so that I can grab all of the images.
What I want to know is if there is anyway for me to grab these values using jQuery and then output them again?
You can use this code (LIVE DEMO) ... you may need to tweak the if() statement a bit to determine which <script>..</script> blocks you want to deal with or not.
$('script').each(function(){
var t = $(this).text();
if (t.indexOf('fNewItem')>0 && t.indexOf('CDATA')<=0){
var a = t.split(/[\r\n]+/); //Split on Line Break
for (var x = 0; x<a.length; x++){
$('#result').append(a[x] + '<br>');
}
$('#result').append('<hr>');
}
});
Related
I am at starting web dev, already using html/css.
For a little project, I had a look at JavaScript. (My goal is that when people click a button, the site will show a random sentence that will be taken from a google sheet cell.)
Could you tell me please if it is even possible? If so, please share some ideas that I will explore. If not, please give me some alternative ideas... Thanks so much.
Have a good day!
-LeganV9
This is possible using Google Apps Script!
I have a working demo here, with the source being here. I dare you to get the jackpot. :D
In order to make this, you can go to https://script.new. Now, in code.gs put this:
function doGet() {
return HtmlService.createTemplateFromFile("index").evaluate().setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
function getVals(){
var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1IDbhQhaImcQB-4j-iByajwAkvxkutptcPMhMTxNrPtU/edit#gid=0");//Put your URL here
var sheet = ss.getSheetByName("Sheet1");//Put your sheet name here
var AMOUNT_OF_SENTENCES = sheet.getMaxRows().toString().replace(".0","");//You can replace this with a number eg 20
var range = sheet.getRange(1, 1,AMOUNT_OF_SENTENCES);
var values = range.getValues();
var newValues = [];
for(var i = 1; i<values.length;i++){
if(values[i][0] === "" || values[i][0] === " "){
}else{
newValues.push(values[i][0]);
}
}
return {valuesVar: newValues };
}
After that, create a new HTML file called "index" and put this in it:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>
The results are: <span id = "results">Loading...</span>
</h1>
<button id = "yourIdHere">Click me!!</button>
<script>
var yourDataList;
function onSuccess(data) {
yourDataList= data.valuesVar;
}
google.script.run.withSuccessHandler(onSuccess).getVals();
var myBtn = document.querySelector("#yourIdHere"); //Declare button, replace yourIdHere with your ID
var randomNum = 0; //Decclre random number
function getRandom() { //Declare your function
randomNum = parseInt(Math.random() * yourDataList.length); //Get random number between 0 and the length of your datalist
return yourDataList[randomNum]; //Return your value
}
myBtn.addEventListener("click", function() { //Bind a listener to the button
document.querySelector("#results").innerText = getRandom(); //Do whatever you want to with the random value
});
document.querySelector("#results").innerText = getRandom();//Change from loading...
</script>
</body>
</html>
Welcome to the world of web development! Hope your project is a success.
It should definitely be possible, since Google Sheets offers an API which has read/write functionality (https://developers.google.com/sheets/api).
You could even later extend this so people can submit their own sentences, given that writing to a Google Sheet is also possible with this API.
However, since you're starting out, consider treating this as an iterative process. You don't have to publish your first version, but just to prevent overwhelming yourself, you might want to set small milestones along the way - each adding more functionality. For example:
Create an array of random sentences (you could, for example, start with using this to keep it simple: https://github.com/JamesFT/Database-Quotes-JSON).
Select and log a random sentence to the console (console.log()) each time the script is executed.
Transfer the random sentence to render in HTML and allow a new sentence to be generated each time a button is pressed.
Move your sentences into a Google Sheet and begin exploring the API.
This way, you achieve something in a much shorter space of time, while working towards your end goal. It's a good way to keep motivated and make things more manageable.
Best of luck!
I have a large amount of text from which I need to find the most recurring words. I was thinking of using a hash table for that.
Issue is, I obtain this text by using a JavaScript API and I am a newbie at that. Is it a good idea to create hash tables in JS and do all of this or will that be very inefficient?
I come from C++/Java, so it's fairly easy to do it there, but the API I'm using is easier in JS.
Use javascript objects.
var myHash = {};
var textArr = ["Mango","Apple","Orange","Grapes","Orange","Apple","Apple"];
textArr.forEach(function(fruit){
if(myHash.hasOwnProperty(fruit)){
myHash[fruit]++;
}
else {
myHash[fruit] = 1; }
});
alert(JSON.stringify(myHash));
I am developing a Chrome Extension that, when the user leaves the page, saves all the text from the textboxes on that page and outputs it to a file.
If I knew the IDs of the textboxes on the page, then it wouldn't be a problem, but the issue is that I need the extension to get the values of all of the textboxes on the page without knowing the IDs, as it will be a different website each time the extension is used.
Also, how would the information be collected? In a string? It would be nice to go down the page and add each textbox to a file, one by one, instead of one huge string.
I've never used jQuery or understood it, and there's probably a solution in it staring me in the face. If anyone suggests using it, please could you explain it a little bit?
Thanks in advance. I would post my code, but I don't know where to start - ergo I don't have any.
you could store it in array using $.each, as :
var valsArr = [];
$("input[type=text]").each(function() {
valsArr.push( $(this).val() );
});
or create object with name as key and value as its value, like:
var valsObj = {};
$("input[type=text]").each(function() {
valsObj[this.name] = $(this).val();
});
You can do it like this:
function onClick(){
var areas = document.getElementsByTagName("textarea");
for(var i = 0; i < areas.length; i++){
alert(areas[i].value);
}
}
<textarea></textarea>
<textarea></textarea>
<button onclick="onClick()">Gather information</button>
Also see this regarding your "save to a file" question Using HTML5/Javascript to generate and save a file
Use the selector and apply it in an each cycle.
$(":text").each(function(){
alert($(this).val());
});
Make a for loop
for(i=0; i < $("input[type='text']").length; i++){
$("input[type='text']").index(i).value();
}
You can use .map() : It returns an array.
var arr = $(":text").map(function() {
return this.value
}).get(); //add join(',') after get() to get a simple comma separated list.
Instead of input[type="text"] you could also use :text pseudo selector.
Demo
I have been trying to find a way to replace a carriage return with the html break. I think it is cr13? Not entirely sure. I am new to programming. I have a content page. So i am setting the javascript in the content area. I have many other javascripts in the page. One hides and shows columns. So i have a way to find the columns. But i think i need to find the cells? It is only one column where this is needed. I am using asp.net, with vb.net, importing a sql server 2008 db. Unfortunatly this must be done in just javascript to avoid page reloads on the click of a button.
Thanks.
function showComments() {
Begincol_num = 8
Endcol_num = 9
Commentcol_num = 7
rows = document.getElementById("<%=GridView1.ClientID%>").rows;
for (i = 0; i < rows.length; i++) {
rows[i].cells[Begincol_num].style.display = "none";
rows[i].cells[Endcol_num].style.display = "none";
rows[i].cells[Commentcol_num].style.display = "";
}
}
The idea is put the js to replace the cr with br within this function(if possible). I am at a loss as to where to start to call the replace in the comment cells. There are 30 comment cells, in the comment column at the moment and will only grow as it goes. This function is called on the click of a button that is not meant to return to the server, which is what i meant by only js.
I am trying to be as clear as possible. I know vb and asp but js makes no sense to me.
Thank you for the help.
In javascript, new lines are represented by "\n", the new line character. If you want to replace a new line with a <br />, then use the string replace function
var stringWithBR = stringVarName.replace("\n", "<br />");
Assuming your comments are contained directly inside the cells with no other tags in between, you could do this:
// ...
for (i = 0; i < rows.length; i++) {
rows[i].cells[Begincol_num].style.display = "none";
rows[i].cells[Endcol_num].style.display = "none";
var commentCell = rows[i].cells[Commentcol_num];
commentCell.style.display = "";
commentCell.innerHTML = commentCell.innerHTML.replace("/\n/g", "<br />");
}
// ...
But it is not a clean solution to do such a thing on showing the comments at all. You should do it either directly on the server or else on load. Otherwise, if you show and hide the comments more than once, you will do some unnecessary work which may not be a performance- but definitely a design problem.
After all problems could occur if there are line breaks inside HTML tags inside a comment. You can prevent that by using innerText and textContent but that would erase any tags. Better solutions are complicated.
I am stumped and could really use some help with this gallery I've been working on. I used Ivan's '4 lines of jquery gallery' tutorial to get me where I am at currently. Here's his demo which shows exactly how it all works http://workshop.rs/demo/gallery-in-4-lines/
I've hit the point where I would like to include a previous and back button.
As the images are named '1-large.jpg','2-large.jpg','3-large.jpg'... etc I tried using the .slice() to take the first digit then add 1 or minus 1 to it, resulting in the next/previous pic but that didn't work well at all as my javascript skills are lacking and I don't even know if it's the best way to approach it.
My question is - Is using .slice() the way to go or is there a more simpler code I can use on my buttons?
Any help would be much appreciated.
If you just want the first character of a string:
var name = "1-large.jpg";
var i = name[0];
// i is now '1'
but this won't work for i > 9, so using split would be better:
var i = name.split('-')[0];
// i is now '1'
var i = "1023-large.jpg".split('-')[0];
// i is now '1023'
and to convert string to int:
var num = parseInt("23", 10);
// num is now the number 23, not a string