jQuery plugin set image src relative to the plugins location











up vote
0
down vote

favorite












I'm having some trouble creating a jQuery plugin that uses an image from the plugin itself.



Let's say I have the following structure




  • foobar


    • assets


      • images


        • fooBar.png





    • js


      • foo_bar.js








And I created a plugin like this.



(function ($) {
$.fn.fooBar = function () {
let image_foo = '<img id="leFoo" style="display: none" src="../assets/images/fooBar.png"/>';
$('body').append(image_foo);
}
});


The plugin is being added to the site correctly and I can call the function like this: jQuery('body').fooBar();. The issue is that it is trying to fetch the image from the current path. So if I was on https://example.com/some/long/path/here.html
It would then try to fetch the image like so https://example.com/some/long/path/assets/images/fooBar.png



While the actual location could be different depending on where the library is being placed. Is there a way to dynamically fetch the path from the library location?










share|improve this question
























  • Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
    – charlietfl
    Nov 9 at 13:28












  • In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
    – Bram
    Nov 9 at 13:30








  • 1




    OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
    – charlietfl
    Nov 9 at 13:32












  • Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
    – Bram
    Nov 9 at 13:35








  • 1




    Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
    – charlietfl
    Nov 9 at 13:37

















up vote
0
down vote

favorite












I'm having some trouble creating a jQuery plugin that uses an image from the plugin itself.



Let's say I have the following structure




  • foobar


    • assets


      • images


        • fooBar.png





    • js


      • foo_bar.js








And I created a plugin like this.



(function ($) {
$.fn.fooBar = function () {
let image_foo = '<img id="leFoo" style="display: none" src="../assets/images/fooBar.png"/>';
$('body').append(image_foo);
}
});


The plugin is being added to the site correctly and I can call the function like this: jQuery('body').fooBar();. The issue is that it is trying to fetch the image from the current path. So if I was on https://example.com/some/long/path/here.html
It would then try to fetch the image like so https://example.com/some/long/path/assets/images/fooBar.png



While the actual location could be different depending on where the library is being placed. Is there a way to dynamically fetch the path from the library location?










share|improve this question
























  • Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
    – charlietfl
    Nov 9 at 13:28












  • In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
    – Bram
    Nov 9 at 13:30








  • 1




    OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
    – charlietfl
    Nov 9 at 13:32












  • Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
    – Bram
    Nov 9 at 13:35








  • 1




    Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
    – charlietfl
    Nov 9 at 13:37















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm having some trouble creating a jQuery plugin that uses an image from the plugin itself.



Let's say I have the following structure




  • foobar


    • assets


      • images


        • fooBar.png





    • js


      • foo_bar.js








And I created a plugin like this.



(function ($) {
$.fn.fooBar = function () {
let image_foo = '<img id="leFoo" style="display: none" src="../assets/images/fooBar.png"/>';
$('body').append(image_foo);
}
});


The plugin is being added to the site correctly and I can call the function like this: jQuery('body').fooBar();. The issue is that it is trying to fetch the image from the current path. So if I was on https://example.com/some/long/path/here.html
It would then try to fetch the image like so https://example.com/some/long/path/assets/images/fooBar.png



While the actual location could be different depending on where the library is being placed. Is there a way to dynamically fetch the path from the library location?










share|improve this question















I'm having some trouble creating a jQuery plugin that uses an image from the plugin itself.



Let's say I have the following structure




  • foobar


    • assets


      • images


        • fooBar.png





    • js


      • foo_bar.js








And I created a plugin like this.



(function ($) {
$.fn.fooBar = function () {
let image_foo = '<img id="leFoo" style="display: none" src="../assets/images/fooBar.png"/>';
$('body').append(image_foo);
}
});


The plugin is being added to the site correctly and I can call the function like this: jQuery('body').fooBar();. The issue is that it is trying to fetch the image from the current path. So if I was on https://example.com/some/long/path/here.html
It would then try to fetch the image like so https://example.com/some/long/path/assets/images/fooBar.png



While the actual location could be different depending on where the library is being placed. Is there a way to dynamically fetch the path from the library location?







jquery jquery-plugins






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 13:33

























asked Nov 9 at 13:25









Bram

1,13652339




1,13652339












  • Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
    – charlietfl
    Nov 9 at 13:28












  • In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
    – Bram
    Nov 9 at 13:30








  • 1




    OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
    – charlietfl
    Nov 9 at 13:32












  • Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
    – Bram
    Nov 9 at 13:35








  • 1




    Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
    – charlietfl
    Nov 9 at 13:37




















  • Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
    – charlietfl
    Nov 9 at 13:28












  • In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
    – Bram
    Nov 9 at 13:30








  • 1




    OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
    – charlietfl
    Nov 9 at 13:32












  • Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
    – Bram
    Nov 9 at 13:35








  • 1




    Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
    – charlietfl
    Nov 9 at 13:37


















Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
– charlietfl
Nov 9 at 13:28






Is there only one assets/images directory regardless of page location? Try removing the leading dots to make it absolute path .. src="/assets/images..."
– charlietfl
Nov 9 at 13:28














In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
– Bram
Nov 9 at 13:30






In the plugin folder "foobar" yes. I can imagine that some sites have multiple directories. To your edit, src="/assets/images... won't be possible since the plugin could be placed in someones site in a different location. E.g. mycool_site/plugins/jquery/foobar
– Bram
Nov 9 at 13:30






1




1




OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
– charlietfl
Nov 9 at 13:32






OK I think I get it, is relative to where plugin is. Might need to set an option for path to images in plugin
– charlietfl
Nov 9 at 13:32














Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
– Bram
Nov 9 at 13:35






Like this then perhaps? jQuery('body').fooBar('/plugins/jquery') where the plugin itself would then use '/' + location + '/assets/images/fooBar.png' ? (location obviously being taken from the options being passed to the function).
– Bram
Nov 9 at 13:35






1




1




Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
– charlietfl
Nov 9 at 13:37






Something like that. Is more common for jQuery plugins to pass in an object.... jQuery('body').fooBar({imageRoot:'/plugins/jquery/'}). Then that object can have numerous options user can override if needed
– charlietfl
Nov 9 at 13:37



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226567%2fjquery-plugin-set-image-src-relative-to-the-plugins-location%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226567%2fjquery-plugin-set-image-src-relative-to-the-plugins-location%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff