Fill in some from HTML form slots using link from another page? - forms

I have a fairly vanilla HTML page with an (inquiry) form. That form has topic field. I'd like to be able to link to that page from another topic-specific page (using an A tag?) on the website, causing that topic field (and maybe some subset of other fields) to be filled in automatically.
Suggestions?

Add a custom param (topic name) to the page where the link points and use it as a subject/topic field.
like:
link
Then, on a target page (with inquiry) use this javascript:
<script type="text/javascript">
// Read a page's GET URL variables and return them as an associative array.
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var params = getUrlVars();
document.getElementById('your_topic_field_id').value = params['topic_title'];
</script>

Related

get value for specific question/item in a Google Form using Google App Script in an on submit event

I have figured out how to run a Google App Script project/function on a form submit using the information at https://developers.google.com/apps-script/guides/triggers/events#form-submit_4.
Once I have e I can call e.response to get a FormResponse object and then call getItemResponses() to get an array of all of the responses.
Without iterating through the array and checking each one, is there a way to find the ItemResponse for a specific question?
I see getResponseForItem(item) but it looks like I have to somehow create an Item first?
Can I some how use e.source to get the Form object and then find the Item by question, without iterating through all of them, so I could get the Item object I can use with getResponseForItem(item)?
This is the code I use to pull the current set of answers into a object, so the most current response for the question Your Name becomes form.yourName which I found to be the easiest way to find responses by question:
function objectifyForm() {
//Makes the form info into an object
var myform = FormApp.getActiveForm();
var formResponses = myform.getResponses()
var currentResponse = formResponses[formResponses.length-1];
var responseArray = currentResponse.getItemResponses()
var form = {};
form.user = currentResponse.getRespondentEmail(); //requires collect email addresses to be turned on or is undefined.
form.timestamp = currentResponse.getTimestamp();
form.formName = myform.getTitle();
for (var i = 0; i < responseArray.length; i++){
var response = responseArray[i].getResponse();
var item = responseArray[i].getItem().getTitle();
var item = camelize(item);
form[item] = response;
}
return form;
}
function camelize(str) {
str = str.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()#\+\?><\[\]\+]/g, '')
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
//Use with installable trigger
function onSubmittedForm() {
var form = objectifyForm();
Logger.log(form);
//Put Code here
}
A couple of important things.
If you change the question on the form, you will need to update your
code
Non required questions may or may not have answers, so check if answer exists before you use it
I only use installable triggers, so I know it works with those. Not sure about with simple triggers
You can see the form object by opening the logs, which is useful for finding the object names

I am getting 0 values for my array when I try to add DOM elements from a webpage, how to add values to lists in .each function?

On console it presents just empty array and 0's for all the elements on the Business Insider page with the specific selectors. How can I add each number (which is views on their site) as a list or string of numbers and then .length on the variable and push it to my html interface?
var request = require('request');
var cheerio = require('cheerio');
var bilist = new Array();
//var x = document.getElementById('bi-stats').innerHTML;
var url = 'http://www.businessinsider.com/moneygame';
request(url, function(err,resp,body)
{
if (err)
throw err;
$ = cheerio.load(body); //create the dom object string
$('span.hot').each(function() {
$(this).text().append(bilist);
console.log(bilist);
console.log(bilist.length);
});
});
function start() {
window.addEventListener('load', bi_list(), false);
}
So jQuery has a different syntax for the 'for' iterator.
The correct code is shown below which appends the text element that contains # of views of the site Business Insider.
$('span.hot').each(function(i, elem) {
bilist[i] = $(this).text();
});

jsTree customize <li> using the alternative JSON format along with AJAX

I am using the alternative JSON format along with AJAX to load data in tree. Now there is a new ask, I am required to add a new element at the end of <li> tag.
I have created sample URL to display what I am currently doing.
Tree crated using alternative JSON format along with AJAX
And how the new LI should appear
Tree created using hard coded HTML but shows how the LI should look like
I think I should be able to do this if I use HTML Data but since the project is already live with JSON format this would require me to change a lot so before I start making this major change I just wanted to check if this is possible using JSON and AJAX format or not.
So I got answer from Ivan - Answer
In short there is misc.js in the src folder which has question mark plugin, this is the best example of what I wanted to do.
I tweaked its code for my needs and here is the new code.
(function ($, undefined) {
"use strict";
var span = document.createElement('span');
span.className = 'glyphicons glyphicons-comments flip jstree-comment'
$.jstree.defaults.commenticon = $.noop;
$.jstree.plugins.commenticon = function (options, parent) {
this.bind = function () {
parent.bind.call(this);
};
this.teardown = function () {
if (this.settings.commenticon) {
this.element.find(".jstree-comment").remove();
}
parent.teardown.call(this);
};
this.redraw_node = function (obj, deep, callback, force_draw) {
var addCommentIcon = true;
var data = this.get_node(obj).data;
//....Code for deciding whether comment icon is needed or not based on "data"
var li = parent.redraw_node.call(this, obj, deep, callback, force_draw);
if (li && addCommentIcon) {
var tmp = span.cloneNode(true);
tmp.id = li.id + "_commenticon";
var $a = $("a", li);
$a.append(tmp);
}
return li;
};
};
})(jQuery);

Json.Net object passed to View in ViewData

I have a page in which I've implemented an ajax call to a controller that returns search results that are packaged in a JsonNetResult.
I have a lot of javascript to then deal with the return package, iterating over each item in the results set and outputting to the page.
Now, however, I need to implement the usage of these same methods for a single item that will be known on page load. I'd like to just pass this info to the View in a ViewData dictionary.
However, I don't know how to use Json.Net in this way.
How do I load a Json.Net object into a native json variable on page load, assuming that object has been stuffed into ViewData["DeepLinkedMessage"]?
I started to go down the path of this:
var thisMessage = (from userMessageProduct .... more linq2sql stuff...);
bool success = (thisMessage.Count() == 0) ? false : true;
var returnPackage = new { success = success, results = thisMessage };
JavaScriptSerializer serializer = new JavaScriptSerializer();
ViewData["DeepLinkedMessage"] = serializer.Serialize(returnPackage);
It worked just fine. But I quickly noticed I'd have to solve the date formatting problems that I'd already solved when I set up the ajax call that returned JsonNetResult. I want to use the exact same methods that I'm using now.
So, I started down this path:
JsonNetResult jsonNetResult = new JsonNetResult();
jsonNetResult.SerializerSettings.Converters.Add(new IsoDateTimeConverter());
jsonNetResult.Data = returnPackage;
ViewData["DeepLinkedMessage"] = jsonNetResult.ToString();
Then, in my view:
<script type="text/javascript">
deepLinkedMessageId = '<%=ViewData["DeepLinkMessageId"] %>';
deepLinkedMessageRaw = '<%=ViewData["DeepLinkedMessage"]%>';
</script>
But this is returning this:
<script type="text/javascript">
deepLinkedMessageId = '1';
deepLinkedMessageRaw = 'WebUI.Controllers.JsonNetResult';
</script>
While I'm looking for something like this:
<script type="text/javascript">
deepLinkedMessageId = '1';
deepLinkedMessageRaw = '{"success":true,"results":[{"UserId":1,"InternalId":"1356935180","FirstName":"Scott","LastName":"Roberson","MessageId":1,"MessageText":"i just love your product!!!", "MessageCreateTime":"\/Date(1295289549930)\/","ProductId":5,"Flavor":"Almonds","ActivePixel":false,"ActiveClass":null,"TileNumber":0}]}';
</script>
Help using Json.Net for this?
Thanks.
Well, I found out from here that I can just do this:
bool success = (thisMessage.Count() == 0) ? false : true;
var returnPackage = new { success = success, results = thisMessage };
ViewData["DeepLinkedMessage"] = JsonConvert.SerializeObject(returnPackage, new IsoDateTimeConverter());
<wipes hands> "problem solved!"
You should set your view to implement your model explicitely:
Inherits="System.Web.View<YourModelName>"
Then in your view you can do
Model.Property
I just find this easier to isolate where the problem is happening (plus viewdata can be a pain in the ass)

Pass parameter to GWT bootstrap .nocache.js script

Is there any way to pass parameters to the .nocache.js script file generated by GWT and evaluate them in the onModuleLoad function? Like so:
<script type="text/javascript" src="application/Application.nocache.js?appId=461333815262909"></script>
The host page URL should be completely separated from the GWT stuff working inside, so passing the appId parameter as a query parameter for the host page and accessing it with Window.Location.getParameter is not an option. I know that I could hide such parameters e.g. in hidden DIVs and then query them from the script, but if it's possible, I'd love to avoid any further dependency in the host page.
Thanks!
Lisa
Instead of hiding information in hidden divs which could get messy, an easy way to pass arguments is via the HTML meta tags.
In the HTML page that calls the GWT script, add a meta tag as follows:
<html>
<head>
<meta name="appId" content="461333815262909">
...
Then, in your module's entry point, parse it as follows:
#Override
public void onModuleLoad() {
NodeList<Element> metas = Document.get().getElementsByTagName("meta");
for (int i=0; i<metas.getLength(); i++) {
MetaElement meta = (MetaElement) metas.getItem(i);
if ("appId".equals(meta.getName())) {
Window.alert("Module loaded with appId: " + meta.getContent());
}
}
}
Granted, it's not quite as simple as passing the argument in the src URL of the script tag but I believe it to be a bit cleaner than hiding divs in the document content and less error-prone than artificially re-parsing the script tag's source attribute.
No, but this article may be helpful in passing parameters from the server to the client-side script for evaluation on page load.
There appears to be no native support in GWT for that, but I came up with the following solution lately:
Assuming that your script always follows the naming convention "/<moduleName>.nocache.js", you can fetch all <script> elements from the host page and search for the one which references this in the src attribute. You can then pull the URL-encoded attributes from there.
Here's my sample implementation, meant to be called with GWT.getModuleName() as the first parameter.
/**
* Fetches a parameter passed to the module's nocache script.
*
* #param moduleName the module's name.
* #param parameterName the name of the parameter to fetch.
* #return the value of the parameter, or <code>null</code> if it was not
* found.
*/
public static native String getParameter( String moduleName, String parameterName ) /*-{
var search = "/" + moduleName + ".nocache.js";
var scripts = $doc.getElementsByTagName( "script" );
for( var i = 0; i < scripts.length; ++i ) {
if( scripts[ i ].src != null && scripts[ i ].src.indexOf( search ) != -1 ) {
var parameters = scripts[ i ].src.match(/\w+=\w+/g);
for( var j = 0; j < parameters.length; ++j ) {
var keyvalue = parameters[ j ].split( "=" );
if( keyvalue.length == 2 && keyvalue[ 0 ] == parameterName ) {
return unescape( keyvalue[ 1 ] );
}
}
}
}
return null;
}-*/;
Suggestions for improvement are welcome.