I've written a function that "condenses" a string if it is too long.
function shortenMsg(msg,maxLen){
if (msg.length > maxLen){
var over = msg.length - maxLen, // amount that needs to be trimmed
[Code]....
Each of the methods 1-3 is the amount that that specific method can trim from the string. I'd like to be able to trim as little as possible.
For example, if the string needs 5 characters to be trimmed, and method1 can trim 8 characters, but method3 can trim 6, then method3 should be used. If none of the methods can individually trim the string enough, then I'd like the optimal combination of the methods that will get the job done.
I can't figure out what sort of code structure I need for this (besides a ton of if/else statements). Maybe an array that contains each of the methods, arranged in increasing order....?
I have a simple hide/show script that is doing what I want, but I was wondering if there is an easier way around having to specify show and hide requirements "onclick" each time. Here is what I've got:
Javascript:
<script type="text/javascript"> function show(boxid) { document.getElementById(boxid).style.display="block"; }
<!-- hide from browsers that dont support js if(document.images) { about_over = new Image about_over.src = "images/btn_about_r.gif" about_out = new Image about_out.src = "images/btn_about.gif"
success_over = new Image success_over.src = "images/btn_success_r.gif" success_out = new Image success_out.src = "images/btn_success.gif" } // -->
and i am using it from within the html code in a standard way:
question: i don't know much about javascript. i know that the script above preloads the images but i don't understand why the images reference names ( "about_over", "about_out", etc) do not get utilized within html code. it seems to me that they should be used.
I am working on a list which displays a large number of contacts (400 to 500 for a typical user). Currently, I am using Dojo (customized widgit) which is created 400 times (once for each contact).This of course is resulting in alot of rendering delay. What is the best approach to display large lists in HTML/javascript? Each list item needs to have an image.
i am trying to store some shopping cart details in a cookie. I have been able to do that but i want to now retrieve the values and put them in a shopping cart table. An example of the cookie file is
I have a website using jQuery that for the most part works fine, but it contains a very large form with a lot of fields. I have an option to save the settings from the form and to load and retrieve them from the server. The problem I'm running into is that the loading settings involves changing so much in the DOM (the form is huge and contains a lot of fields) that it seems to be freezing up on some browsers or timing out. I can't reproduce this on my computer (although it does take awhile to finish processing) but I've gotten enough reports of the problem that I'm looking for some advice as to how I can speed it up some.
The site is [URL] I've tried to speed it up by caching a good chunk of the selectors I'm interacting with and I'm using IDs to access the fields in most circumstances. But I'm not sure what else I can do to really optimize the loading. how I can go about improving on the load speed?
I'm building on-the-fly <select> lists from JSON data fetched from the server. Some of then include a large number of items (>20,000).
The SQL and HTML parts are working fine. The AJAX script fetches data fairly quickly (around 1 second) and large selects are not a problem once they're built (the browser handles them nicely, even IE). The bottleneck is in the process of picking the JSON data and building the <option> tags. That can take a full minute.
What's the recommended (i.e. fastest) method to generate a large <select> list?
My current approach is this:
// Fetch data (GET method allows me to use browser cache) $.get(url, get, function(jsonValues, txtStatus){ that.values = jsonValues; }, "json");
I want obsfucate and compress the code for my website (HTML, CSS but especially Javascript). Are there any industry standard algorithms that are recommended?
Does anybody know of an algorithm to calculate mortgage payments? e.g.
Amount: 100,000 Interest Rate: 4.5% Term: 25 Years
Monthly Payments: x
I've looked at other js scripts to do this but it seems alot more involved then I thought, and because they use non descriptive variable names I can't work any of them out..
So I'm using a little piece of JavaScript code in which im accessing the AStar function. The downside is, the AStar algorithm only finds vertical tiles. (See screenshots below)
function mapmouse (obj) { //Get the coordinates coords = obj.id.split('_');
[code]....
If you choose a non-vertical path, var path gives an empty array (path.length = 0 )If I choose a vertical path, it's working as intended.
As you can see with this little test here, the AStar function itself is working properly (the libs.js is used by both the game and the example)[URL]
I have an assignment to code a java class that plays rock, paper, or scissors. The class can access the history of past gestures played by both itself and its opponent, but nothing else. The class will be played against others in 10,000 matches and the winner will be determined via round robin format. Other than using a greedy algorithm to determine statistically the best choice from prior gestures and basic pattern recognition I have no decent ideas.
I need a simple, quick and efficient way to logically branch if I find a string is contained in another string in jquery Most other languages this can be resolved in one or two lines and it would be readable.
I have a simple example below showing how when I pass in the value of the value attribute of option node, and then use if operator to check whether parameter is a string or not, even though it's a string, it converts it to false boolean and triggers the else statement rather than calling a function.callback should be a string so why is it saying otherwise?
I have made a basic form, and I need to combine three values within my form, then create an md5 hash of this string.Then assign it to a hidden variable.My form is here...
Or I have created a pastebin of it here, for easy reading: http://pastie.org/1171757.So I need to be able to combine the three values into a string, create a md5 of the string, then call the value of the string into a hidden value all before posting the form.
I need help with substring or trim function in javascript. Find below my code. Selection holds the value Select State, and length of the string is 14. I need to equate the Selection value to string "Select State" and execute alert message.
function selected_item() { if (Selection=="Select State") alert("Select the State");[code]....
I tried this:
var state=Selection.substring(0,11); and then string would be equated to state variable. But it is not working.
I am currently trying to build a new function in javascript that is supposed to handle a string of text. The idea is that it should find the lowercase letters and uppercase letters in a string and then swap them.
Meaning all lowercase letter becomes uppercase letter and vice versa.
So i am just asking if someone could point me in the right direction or give some tips. I've currently been reading about the toUpperCase(); and toLowerCase(); functions and i am fairly confident i know how to use them for switching, however i still need a way to find the lower , upper character in the string so i later can switch them.
<script type="text/javascript"> var str="Welcome to Microsoft! Microsoft Microsoft"; var stringToBeFound = 'Microsoft' var ReplaceString = 'site' document.write(str.replace(stringToBeFound , ReplaceString )); </script>
My problem is im trying to use string.replace that is not case sensitive and replace every string found. I could use regular expression with it but my stringToBeFound is a dynamic variable im getting it from my database
How can i remove a string from an existing string in javascript. I have a textbox in a form and want to make sure that when the user clicks a button that certain words are moved, like all instances of "hello" should be taken out of the text the user typed in the textbox.