简体   繁体   中英

Javascript Bookmarklet - remove spaces from input

I am pretty new to this so please take it easy on me.

I have a javascript bookmark that I use to look up tracking number for parcels on Purolators website (Canadian shipping company). What I want it to do is to take the input (tracking number) and remove any spaces in it before opening the URL. The tracking site is stupid and uses spaces as delimiters for new tracking numbers.

Also, as a bonus, can this be made to open in a new tab?

javascript:var%20trackID%20=%20escape(prompt('Enter%20Tracking%20#'));window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='%20+%20trackID;

I have tried adding trackID%20=%20trackID.replace(/\\s+/g,%20''); before the window.location but no luck.

Any ideas?

You don't need to use escape in this scenario. This is actually replacing your spaces with %20 special characters which is why your replace wasn't working

Check http://www.w3schools.com/jsref/jsref_escape.asp for more info.

Your corrected code should be something like

javascript:var trackID=prompt('Enter%20Tracking%20#').replace(/ /g,"");window.location='https://eshiponline.purolator.com/SHIPONLINE/Public/Track/TrackingDetails.aspx?pin='+trackID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM