Advanced techniques for reducing the page weight of hidden HTML forms - html

I'm working on a large application with lots of editable objects on a page. One page for example contains a list of categories and each category has a list of tasks. Both categories and tasks can be editted via a modal dialog window with a form inside.
The current solution is to embed a hidden form prepopulated with data next to each object in the html. When the edit button is clicked, the form is displayed inside the modal dialog and submitted via ajax. The advantage to this solution is that modal dialogs pop up quick. The disadvantage is the growing number of dom elements and page size as each object requires another hidden form.
I am looking for ways to reduce the page weight and reduce the number of dom elements. I am looking for a balance of performance vs maintainability, as there are lots of pages with lots of different objects to consider.
Use ajax to dynamically load the modal populated form. This reduces the page weight considerably, but causes a delay when the edit button is clicked and is the worst for user experience. It also requires each form to have an ajax handler for this specific function.
Store the form parameters as json data and populate the form values from a cloned template form when an element is edited. This reduces dom elements significantly, and the page weight a little as well. A generic piece of javascript needs to be written to insert the json data into input elements.
Store the entire form HTML as a json object inside a data attribute of a dom element. This does nothing for page weight but will reduce the number of dom elements. On edit, the html can be injected into the modal dialog. Its also the easiest to implement.
Scrape the content of the page for the values of the form, and inject them into a cloned template form. This reduces the duplication of content, and does the most to reduce page weight and dom elements. However it feels brittle and intrusive, there needs to be id coordination for content and special care needs to be taken so that hidden input fields are also injected into the page.
Those are the options I've come up with. The JSON solutions are the most appealing to me currently. I'm curious if anyone has other solutions or insight to any of these?

The most light-weight is probably store the entire form definition as JSON and write some sort of engine that would dynamically build the form HTML on the fly. You'd then use another JSON file to represent the data layer that populates/contains the form data.
The fun you run into is how to manage the layout of the form on top of all this. For a standard two-column layout it's simply. Beyond that it's an awful lot of work.
I started writing one using prototype.js about five years ago. I never finished it but it works. Feel free to view the source and steal the code.
If you look at advanced client libraries like ext.js, this is how they're doing it.

If you're going for speed, and don't have a framework handy, don't think "HTML", think "DOM element." Maintain as much data as possible in a "global" data object, and only go to the server for information you don't have. Initialize a modal <div> either on demand or asynchronously after form load, and populate its options via JavaScript in response to a handled event from each <input> element.
For performance, consider storing categorical information in some localStorage option.
Realistically, however, you want a framework. (If you don't use one someone else wrote, you should write your own.) I'm a fan of the Enyo framework leftover from WebOS personally, but there are others out there to help you do this.

Related

Is it possible to validate only parts of a form with different buttons

I have to create a large form that will at the submit at the end.
However since this form is large, it is split into smaller steps. Each step is displayed on the same page but only accessible in order (so first have to complete step 1 before being able to open step 2 etc).
Therefore I need to validate the inputs for each section before giving access to the next.
1.Should I break apart the form (so create 5 forms that each get validated on submit and then have a final button that checks them all and sends them off to backend)
Or
2. can I keep it as one large form and is there a way to only validate certain parts of the form? From what I understand nesting forms is not possible
(I am using jquery)
As you said, the steps are merely a UX improvement to a long form. So stick to one form tag and validate the inputs either on blur / input for each field or when your user is about to move from one step to the next.
You could maintain a small object with the state of each step and upon submit validate that each step is marked as "validated" before sending your request to the backend.
Alternatively, as forms can be tedious to validate properly, have a look at the many libraries out there that could help you in the process.

Using either GET or POST depending on submit button

I have a web application for tagging data and viewing data by tag, so my UI is a list of checkboxes for each tag, a list of checkboxes for each data item, a "Tag" button, which adds the checked tags to the checked data, and a "Filter" button, which ignores the checked data and just displays only the data items with the given tag.
My problem is that the former operation (tagging data) is "obviously" a POST operation, whereas the latter operation (viewing data according to a tag) is "obviously" a GET operation. But the method attribute is attached to the form, not the submit button, so I have to choose one or other for both buttons.
I don't want to make two forms, since as far as I can tell this would force me to duplicate the entire tag list. Is there any way I can choose my method based on the choice of submit button?
A JavaScript solution is permissible, but one without would be preferred.
(I am going to post an answer to this question, but I don't particularly like it, so I would welcome alternatives).
In principle, you could use the formmethod attribute in a submit button, as per HTML5. However, it is not recognized by IE, even in IE 9. The existence of the feature in HTML5 indirectly proves that previous versions of HTML lack a feature for this.
On the other hand, the POST method can be used even for simple viewing that does not cause any changes in the outside world, and in many situations it has to be used for technical reasons (e.g., too much data). So I think the method issue is not very relevant; just use POST.
I would honestly go with a javascript solution, in the onsubmit of the form fire a method which a) checks the submit button that was pressed and b) based on this changes the method of the form.
One possible solution would be to use POST, and then have the server give a 303 See Other header to change it into a GET request. This involves making two requests to serve the purpose of one, which is unfortunate, but at least means that the URL will change so people can link to a specific tag selection.
I agree with javascript solution, proposed by Jon Taylor, the problem is not if your form's method is GET or POST, but how do you filter/validate/sanitize user input. If your concern is related to the fact, that the user can manipulate the form's method, then you should implement solution to that matter on server side.

Using a single shared element across multiple partial views

I have a basic ASP.Net MVC 3 application which has a number of controllers and a number of actions (and subsequently views)
A common feature of the application is to show a pop-up dialog window for basic user input. One of the key features of this dialog process is a faded mask that gets shown behind the dialog box.
Each of these dialog window controls is in a separate Partial View page.
Now, some view pages may use multiple dialog boxes, and therefore include multiple partial views in them - which as is would mean multiple instances of the "mask" element.
What I am trying to find a solution for is to only need to create one instance of a "mask" element regardless of the number of dialog partial views I include, and then the script in each partial dialog will have access to this element (so basically it just needs to be on the page somewhere)
The only real idea I have come up with so far is to add the "mask" element to the master page (or in the original view page) and this will mean it only gets added once. The problem here is that it will be added even when it is not needed (albeit one small single element)
I can live with this, but I would like to know if there is a better way to handle these kinds of scenarios?
A quick idea that came to mind is some kind of master page inheritance hierarchy, So I may have a DialogMasterPage that inherits from the standard current master page. How does that sound for an approach?
Thanks
To do something like this, where each module can register their need for a certain thing in the master page, you can use HttpContext to store a flag of whether you need to write the mask div, and just set that property in each partial. At the end of the master page, if the flag is set, you can then write the mask div if its set to true.
Obviously to make this cleaner you could wrap it all in an HtmlHelper extension or something.
My initial thought is for you to use something like jQuery UI where it handles the masking for you or if you are using something custom you can load the content for the dialog via ajax then show it in the single dialog on the master page.

Creating "are you sure?" popup window by using html only

Assume I have a html from, and it contain some submit type. I want to create a "are you sure" popup window that will appear when user click submit button.
My question is that is there any way to create it by using "only" html, not using javascript or any other?
HTML only is possible, but not without a postback
Scenario that could work without javascript:
You have your form with submit button
User clicks (and submits) the form
You display another form with are you sure? form (that contains Yes and No buttons as well as hidden fields of the first form that will make it possible to do the action required on the original data
functionality that executes the action and goes back to whatever required.
This would be completely Javascript free, but it would require several postbacks.
This kind of thing is usually done on the client with a Javascript confirm() function (here's a simple example) or lately with a more user friendly modal dialog provided by many different client libraries or their plugins.
When to choose the script free version?
If you know your clients are going to be very basic ones (ie. vast majority of your users will access your application using clients like Opera Mini that's not able to run scripts at all). But in all other cases it's much better to do this using Javascript. It will be faster, easier to develop and much more user friendly. Not to mention that it will put less strain on your server as well since certain parts will execute on the client without the need of any server processing.
No, there isn't. Despite of the new features in HTML 5, HTML is still a markup language, not a programming language. In order to express dynamic behavior (such as an "are you sure?" box), you need to use a programming language.
Javascript would be the most obvious choice for this, but you could also do it with frameworks that can get you around writing Javascript by hand (for example ASP.NET).
Edit: Actually it appears that it would theoretically possible to do this with without Javascript or other frameworks. As I just learned, HTML 5 + CSS 3 seems to be turing complete. But this is hardly relevant to this question.
It's possible to ask for a confirmation, but it will not be in a "popup window". The creation of the "popup window" requires javascript/other language.
It will be:
Request (first form)
POST
Response (confirmation form)
POST
Response (outcome message)
You can create a form with all hidden elements containing the data from the first form and a "Yes" and "No" button below the "Are you sure?" text. You can use PHP sessions to avoid the hidden form elements. If there is a lot of data or confidential data or you do not want to re-validate the data from the second form, use sessions. Make sure you validate the data from either form before using it.
I know I'm like .. 10 years late. But for anyone still wondering I thought I could be of some help!
What I did for this exact problem was make sure I had multiple "divs" in my code. For me specifically, I had two main ones.
First, one whose id="main", and another whose id="popup" with the 'visible' property initially set to 'false' for the popup div.
Then, on whichever event you're looking for (button click for example) you'll simply set main.Visible = false and popup.Visible = true, then you could have more buttons in your popup (yes, no, cancel, confirm, etc.) which do the exact same thing, but in reverse!
The most important thing to make sure of is that you have the 'runat="server"' property in your divs so that you can access them in your CS code
Hope this was helpful! :)

How can I track changes in content on an HTML page after page has loaded

I'm wracking my brain on this one.
After an HTML document loads in a browser, I want to be able to monitor
the page in case any content on it changes for any reason.
Is there a Javascript function with which I can track 'what has
changed' on the webpage. This should be irrespective of the type of content on the HTML page.
I have two examples for you to ponder on:
Ex1:
Say in an HTML document there are two select boxes s1 and s2.
The items list in s2 depends on selections in s1 (page is not
refreshed — that is, s2 is loaded through Ajax or something).
So after the HTML page loads I need to get a notification whenever s2
is populated...
Ex2:
Say, in an HTML page, there's a link, Onclicking which a light pop-up
div is created with some text.
How can I capture the content of this dynamic pop-up?
In all this discussion, I'm not taking into account any particular
format of HTML...the HTML content can be anything...I just need
to keep tracking if any content changes after the page loads...
Ideally I need to achieve this using JavaScript (client-side
scripting).
How can I achieve this?
You can keep track of changes in a textbox using onkeyup. This will tell you every time someone makes a change in a given textbox.
This could potentially fire alot of events. However, using onblur won't necessarily tell you about changes in the textbox and onchange's browser coverage is spotty at best.
If you are using AJAX, you could setup the response function to handle a home grown "event listening" system. So after the response does what it needs to do, it could call any methods that were registered with it, passing in the response text when necessary.
So from your examples above, in Ex1, when the AJAX returns from S1, it would load S2, then call a method saying S2 had changed. In Ex2, when the new AJAX returns the DIV's contents, after loading it into the DIV, it call a different method (or possibly the same depending on what your trying to do) and alerts it that the DIV has new contents.
You could set your "watcher" script as a timer, running a diff function on the current document.body.innerHTML and a stored version captured on load. Depending on how fast the diff will run will give you an idea on what timer interval to use.
This may not capture changes in form elements, but for those, it's easier to loop through all form elements in every form on the page.
Here's someone's diff function I found on Google: http://snowtide.com/jsdifflib