homepage Welcome to WebmasterWorld Guest from 54.211.164.132
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Trying to combine two scripts and open in a new window
combine two scripts and open in a new window
RickB




msg:4428740
 7:04 pm on Mar 13, 2012 (gmt 0)

I'm trying to combine these two scripts and get them to open in a new window. Any help / advice that you guys can offer is greatly appreciated. Basicly what I'm trying to do is that once a user imputs the contents into the 1st form field, a second form field will capitalize all the letters from the 1st form field and then convert that part of the form into the url and open the link in a new window. I've been working at this for a while and I can't get this to work correctly.

Here is the first. It works great by itself but for the life of me, I can't get it to open in a new window.

<script>
function convertURL() {
document.go.url.value = document.go.url.value.replace( /\s/g, "");
var url = document.getElementsByName( "url")[0];
var fullurl = document.getElementsByName( "fullurl")[0];
fullurl.value = "http://www.MY-WEBSITE.com/"+ url.value +".pdf";
//msg = document.go.url.value +"?"
//return confirm(msg);
}
</script>
<form name= "go" onsubmit='convertURL();location.href=fullurl.value;return false'>
<input name='url' type='text'/> <input name='fullurl' type='hidden' />
<input type='submit' value="GO" />
</form>


Here is the 2nd.


<script>
function upperMe() {
document.getElementById("output").value = document.getElementById("input").value.toUpperCase();
}
</script>
</head>
<body>
Enter lowercase letters for conversion to uppercase:<br>
<form name="converter">
<input type="text" name="input" id="input"
value="sample" onchange="upperMe()" /><br />
<input type="text" name="output" id="output" value="" />
</form>

 

rocknbil




msg:4429098
 4:32 pm on Mar 14, 2012 (gmt 0)

Welcome aboard RickB,


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
window.onload=function() {
// always use id's and document.getElementById, not name. EASIER! :-)
var frm = document.getElementById('go');
if (frm) {
frm.onsubmit=function() { return convertURL(this,800,700); };
}
};
//
function convertURL(form,w,h) {
var orig_url,day,id,params,full_url,win;
orig_url_field=form.url.value.toUpperCase();
day = new Date();
id = day.getTime();
params = 'width='+w+',height='+h+',scrollbars,resizable';
full_url = 'http://www.example.com/'+orig_url_field.replace( /\s/g, "")+'.pdf';
// for testing
//alert(full_url);
win = open(full_url,id,params);
return false;
}
</script>
</head>
<body>
<form id="go" action="">
<p><input name="url" id="url" type="text"></p>
<p><input type="submit" value="GO"></p>
</form>
</body>
</html>

RickB




msg:4429329
 2:21 am on Mar 15, 2012 (gmt 0)

Thank you, Thank you, Thank you rocknbil. The script works like a charm. I have been trying for a couple of days to get this in place. I do have one other quick question. I'm trying to use a Regular Expression to find a certain word on my webpage. It is a unique word (17 characters,letters and numbers). Each one is different on each page. Can you tell me if this is the correct expression?

/^([a-zA-Z0-9_-]){17}$/

Once again thank you for all your help!

rocknbil




msg:4429625
 4:52 pm on Mar 15, 2012 (gmt 0)

I don't know, does it work or not? :-)



find a certain word on my webpage. It is a unique word


But . . . I have a feeling it still won't match exactly, and may still find other 17 character words (if there are any).

Examine what it does.

^ ... $ = the string you're searching STARTS WITH (^) and ENDS WITH $. If you're searching a whole page, I'm guessing the answer is "no, it doesn't work." This means the whole page must start and end with your pattern and contain nothing else.

/^abc$/ will match 'abc' but not " learn your abc's"

() = probably not needed, this is to group patterns and/or store the match in special variables, i.e. (this)(that) will have "this" in $1 and "that" in $2 (if a match is found.)

[] - character class - "if the pattern contains any of these characters"

a-zA-Z - match any letters. This is more efficient with the case insensitive modifier "i" (but works the same, so is OK . . .)

0-9 = match any digit, identical to \d

_- = characters to match - be cautious with the dash, as you can see above when in a character class it's often used to define a range, so I just always escape it (and the underscore as well. Probably not needed but that's what I do.)

{17} = a quantifier, match *exactly* 17 of the preceding characters.

so, all that being said, a more efficient version might be


/[a-z\d\_\-]{17}/i

But as mentioned, I doubt that's what you're after and may return other 17 character strings. If it's a unique word, why not just match the exact pattern?

var pat = '/aybe_cee-dee_effg/i';

RickB




msg:4429645
 5:47 pm on Mar 15, 2012 (gmt 0)

Thanks for your reply. Here is what I'm looking for and part of my project. The 17 character word is actually VIN #'s for vehicles. Each Vin is unique and it contains letters and numbers in no specific order and they are always 17 characters long. I'm using the revised script you provided along with another script combined with Regex to search for the Vin # on the page, and plug that Vin # into the form field. Each page the script is on will have the same Vin # on it. I've tested the original expression and it does work but the vin # / 17 char. word has to be the first word and only word on the line for that expression to work. Below is a sample of the html on the page the script will be placed on.

<dl>
<dt><span>Bodystyle</span></dt><dd><span>2 door Hatchback</span></dd>
<dt><span>Drivetrain</span></dt><dd><span>FWD</span></dd>
<dt><span>Engine</span></dt><dd><span>1.3L I-4 cyl</span></dd>
<dt><span>Fuel Type</span></dt><dd><span>regular unleaded</span></dd>

<dt><span>Transmission</span></dt><dd><span>Continuously Variable</span></dd>
<dt><span>Ext. Color</span></dt><dd><span>Blizzard Pearl</span></dd>
<dt><span>Stock Number</span></dt><dd><span>000CS210</span></dd>
<dt><span>Vin</span></dt><dd><span>JTNJJXB07CJ011417</span></dd>


<dt><span>Model Code</span></dt><dd><span>6242</span></dd>


</dl>

With my current script, I know the user can copy and paste the vin # into the form field and click go and it will perform as it should. If for some reason the user types in the wrong vin #, then they will get an error page. I already have the script that will highlight the selected text and paste it into the form field from other test that I've done. That part works but, it is for specific characters that I know are in the page (JTNJ)for testing purposes. If I can auto fill that form field for the user based on a click, then I will have less chances for the user getting an error.

RickB




msg:4429691
 7:56 pm on Mar 15, 2012 (gmt 0)

RocknBil,

I got it working! The regex. You were right. Here is the correct expression that works.

/[A-Z0-9]{17}/

This works perfect!

My next step is combining the scripts!

rocknbil




msg:4429997
 4:27 pm on Mar 16, 2012 (gmt 0)

I know the user can copy and paste the vin # into the form field and click go and it will perform as it should.


Right, because the text field starts and ends with the pattern. When used that way, it's a good idea to trim off trailing and leading spaces (i.e., "help the user because they may not know" or may be copying and pasting from Word, which often adds a trailing space.)

The only thing I'd add to your regex is possibly make it case insensitive in the event someone enters it in lower case.

var reg = '/[A-Z0-9]{17}/i';

Optional, 0-9 and \d are identical. :-) The dash defines a a range, so it's appropriate for that - that is, everything between and including 2 to 8 would be [2-8]. Really it's the difference between saying "any digit" and "0 1 2 3 4 5 6 7 8 9" or "zero through nine".

var reg = '/[A-Z\d]{17}/i';

RickB




msg:4431581
 3:02 am on Mar 21, 2012 (gmt 0)

RockNbil,I have another quick question that you may be able to help me with. I have been trying for a couple of days to get another Regex right and for the life of me I can't figure it out. BTW, the Vin# expression works fantastic!

Here is what I have so far. What I'm trying to capture with the expression is pricing. The expression I'm using is (abbr>[0-9,]+). The expression does capture what I want it to but I need it to do it globally. From what I've found online adding a /g is suppose to do just that. In every Regex tester that I've used, it does not work. The expression is basily broken if I do this (abbr>[0-9,]+)/g When I set the tester to check all or global and not use the /g it highlights everything like it is suppose to. I need the expression to check for all the prices on the page. I've used other expressions for pricing but the pages I'm using the expressions on, there are lots of different numbers on the pages. ie: engine size 2.4, vin #'s, jpeg photo links with numbers, etc.. Any advice on how I can accomplish my goal of just capturing pricing? Below is a snip of the website.

<dt class="stockNumberLabel">Stock Number:</dt><dd class="stockNumberValue">U048691</dd>
vehicleId|011f534a0a0d048e01bbe4e1b2a3956b<span>MSRP</span>
<em><abbr title="dollar">$</abbr>23,925</em>
</span>
<span class="price discountAmount">
<span>Discount</span>
<em><abbr title="dollar">-$</abbr>1,635</em>

</span>
<span class="price primaryPrice">
<span>Internet Price</span>
<em>
<abbr title="dollar">$</abbr>22,290
</em>
</span>
<span>MSRP</span>
<em><abbr title="dollar">$</abbr>23,925</em>
</span>
<span class="price discountAmount">
<span>Discount</span>
<em><abbr title="dollar">-$</abbr>1,635</em>

</span>
<span class="price primaryPrice">
<span>Internet Price</span>
<em>
<abbr title="dollar">$</abbr>22,290
</em>
</span>

rocknbil




msg:4431762
 4:49 pm on Mar 21, 2012 (gmt 0)

Here's a a better idea: pay the site you're getting these from for a feed.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved