Javascript - combine variables to form path - javascript

I need to combine two variables and one static string to form a path to may image - see below. Please advise on how to correct the syntax:
var shortUrl = "http://www.art.com";
album_name = "art";
poster:""+shortUrl+""/""+album_name+"".jpg",
I am trying to get it to output:
http://www.art.com/art.jpg

You almost had it.
shortUrl + "/" + album_name + ".jpg";
You don't need to escape slashes when they are inside a string literal.

You should use var to prevent the variables from becoming global, assign the result to poster with an =, remove the opening "", and only use one " on either side of a string.
var shortUrl = "http://www.art.com";
var album_name = "art";
var poster = shortUrl +"/" + album_name + ".jpg";
console.log(poster);

Related

Add "/path/" in the middle of an URL in JavaScript

How can I effectively add a "path" to the middle of an URL in JavaScript?
I want to add embed to an URL, so the URL https://blog.com/post/123 will end up looking like this https://blog.com/embed/post/123?
Cheers
You can create an <a> and set the href property. Then prepend embed to the pathname and use toString() to get the whole URL.
let element = document.createElement('a');
element.href = 'https://blog.com/post/123';
element.pathname = 'embed' + element.pathname;
console.log(element.toString());
You can do this, if the path is just a string
var path = "https://blog.com/post/123";
var withEmbed = path.replace(/\/post\//,'/embed/post/');
console.log(withEmbed);
You can use the location API.
https://developer.mozilla.org/en-US/docs/Web/API/Location
function addEmbed(location) {
return location.protocol + '//' + location.host +
'/embed' + location.pathname;
}
var url = document.createElement('a');
url.href = 'https://blog.com/post/123';
var embed = addEmbed(url);
console.log(embed); // "https://blog.com/embed/post/123"
Example: https://codepen.io/anon/pen/wXxvaq
The way i would do it, is to pass by ref/value the original URL and the text you wan to add, into a function. It then removes the "https://" (if necessary), splits the url at the first "/" and saves each part as a var. Finally it puts it all back together and outputs it to a on the html page.
This doesnt have to be outputted in this way, it could be saved as a global variable and then used in a link (but i didn't know what your plan was so i outputted it) :)
function addToURL(URL, add) {
URL = URL.replace(/(^\w+:|^)\/\//, '');
var part1 = URL.substring(0, URL.indexOf("/") + 1);
var part2 = URL.substring(URL.indexOf("/"), URL.length);
var result = "https://" + part1 + add + part2;
document.getElementById("demo").innerHTML = result;
}
Here's the example I made: https://codepen.io/anon/pen/RJBwZp
Hope this helps :P

Add double quotes to string which is stored in variable

I have variable which i am getting from DB string url, But the url does not have quotes to url i need to add the quotes to it below is my code.
var audioUrl
url is having string like http://xxxxx/xxx/xx-xx-123.m4a without double quotes
audioUrl= (data.url)
i need convert data.url value to "http://xxxxx/xxx/xx-xx-123.m4a"
Circle Jplayer
var audio="http://xxxxx/xxx/xx-xx-123.m4a"
var myOtherOne = new CirclePlayer("#jquery_jplayer_2",
{
m4a: audio,
}
If possible, I'd use ES6 syntax for this:
`"${data.url}"`
var audioUrl = "\""+ data.url+ "\"";
Whatever your get audioUrl and you want to wrap it with ", you need to put them and escape inner ones with . Above will result in:
"http://xxxxx/xxx/xx-xx-123.m4a"
OR if you are using the single quotes then no need to use the escape character.
var audioUrl = '"'+ data.url+ '"';
This is the simplest way
var audioUrl = '"' + (data.url) + '"'
This is a problem that comes up all the time for me and it does get frustrating.
A simple solution is to create global variables and use them in your strings as needed like so:
var singleQuote = " ' ";
var doubleQuote = ' " ';
Hope this helps others.

Search and remove a parameter with optional character from URL

I'm looking to remove a parameter from a URL with a click event. The issue is that the parameter can either have an & before it or not. So the form is either search=MYSEARCHTERM or &search=MYSEARCHTERM.
I have the following which appears to work fine for one or other but not both. I was thinking that I could have an if / else statement one of which contains something like this. (Excuse the crappy regex but I've never written it before)
var searchKeywordRegx = new RegExp(/(?:&)/ + 'search=' + searchKeyword);
$('.searchKeyword').click(function() {
$(this).remove();
var searchKeywordRegx = new RegExp('search=' + searchKeyword);
console.log(searchKeywordRegx);
document.location.href = String( document.location.href ).replace(searchKeywordRegx , "" );
});
Am I way off base here?
Use ? to make something optional in a regexp:
var searchKeywordRegx = new RegExp('&?search=' + searchKeyword);
Seems you can do this without regular expressions. If you simply remove that portion of the document location's "search":
document.location.search = document.location.search
.replace('search=' + encodeURI(searchKeyword), '');

Cuting an URL with JS or Jquery

Hello guys is it possible to throw away the first part of an url
var mydir = $("script[src$='jquery_main.js']").attr('src').slice(0, -14);
http://127.0.0.1:9081/Mgr/resources/ui/skins/default/js/main/
im trying to remove this part "http://127.0.0.1:9081/Mgr/" but without indexOf. is it possible to cut the url after the third / to get just this part /resources/ui/skins/default/js/main/
Not sure why you don't want to use indexOf, you can try this as well
var cutURL = "/" + "http://127.0.0.1:9081/Mgr/resources/ui/skins/default/js/main/".split(/\/+/).splice(3).join("/");
DEMO
var url = "http://127.0.0.1:9081/Mgr/resources/ui/skins/default/js/main/";
var cutURL = "/" + url.split(/\/+/).splice(3).join("/");
alert(cutURL);

Replace ID in a URL between two markers

I have a url which looks like this:
I want to replace: 1034749-184e-3467-87e0-d7546df59896 with another ID, is there any regex or similar replace method which will allow me to replace the ID using JavaScript between the 'image/' and '?' characters?
You could make this approximation expression:
/[0-9a-z]+(?:-[0-9a-z]+){4}/i
Match a bunch of hexadecimals, followed by 4 sections, each starting with a dash followed by a bunch of hexadecimals.
> var s = 'http://url/images/1034749-184e-3467-87e0-d7546df59896?w=600&r=22036';
> console.log(s.replace(/[0-9a-z]+(?:-[0-9a-z]+){4}/i, 'something-else'));
http://url/images/something-else?w=600&r=22036
/images\/[^?]+/ would match, but it would replace images/ as well.
Fortunately you can pass a callback to .replace:
url.replace(/(images\/)[^?]+/, function($match, $1) {
// results in "images/<some-ID>"
return $1 + theNewId;
});
If you have a reference to the DOM element anyway, you can also just replace the last step of the path:
element.pathname =
element.pathname.substring(0, element.pathname.lastIndexOf('/') + 1) + newId;
Yes, just do this:
var url = document.getElementById("yoururlid").src;
url = url.split("/");
var newUrl = url[0] + url[1] + url[2] + url[3] + newURLID;
Why not just do this:
document.getElementById("yoururlid").src="http://url/images/" + newId + "?w=600&r=22036";

Categories

Resources