ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to describe an image file on javascript file through codeigniter?

March 27, 2013 11:24am

Subscribe [2]
  • #1 / Mar 27, 2013 11:24am

    queencode

    2 posts

    I’m new using codeigniter. I using codeigniter 2.X. I have some question regarding basic course of codeigniter.
    1. What the best suggestion for folder location of our external css/js/image/another ? For now, i put js/css/or another file on the root, i.e.

    site url/css

    2. How to describe path of image on css and javascript file ? On php its easily that just use <base_url> but how in css and javascript?
    Both question I need for the best and experience answer because the answer will be my knowledge base for using codeigniter. Thank you so much.

  • #2 / Mar 27, 2013 1:41pm

    Otemu

    406 posts

    Hi

    1. Typically you should have a structure like below:

    +www root
    ++application
    ++system
    ++assets
    +++css
    ++++style.css
    +++scripts
    ++++script.js
    +++images
    ++++myimages.jpg

    Then typically you have a template view page check out this tutorial and load your css, js, images etc there, using baseurl is fine there is a few ways you can do it check out this post for some other methods

    2. For css I normally use relative urls, for instance with the structure above if I wanted to set a background image(myimages.jpg) within style.css. I need to go up a directory then down into images folder, so it would be background:url(../images/myimages.jpg);

    For JavaScript normally I have something like var siteUrl = ‘<? base_url(); ?>’; within the head tags before any JS inline/external is loaded, then when ever I need to use a path in my external js I can just use the variable siteUrl as my base.

    Hope that helps

  • #3 / Mar 27, 2013 8:46pm

    queencode

    2 posts

    Thank you for your answer. Regarding the second question, how to put php code on javascript like your hand? I already searching on google that that’s not good behavior “I wouldn’t recommend this behavior” ? Need your assistance please

  • #4 / Mar 28, 2013 1:48pm

    Otemu

    406 posts

    Actually that is not what I mean, you won’t be using php in the external js file at all your just do something like this

    someview.php

    <html>
    <head>
        <title>your title</title>
    <js-tag>
    var siteUrl = ‘<? base_url(); ?>’;
    </js-tag>
    <js-tag src="somejs.js"></js-tag>
    </head>
    <body>
    <div id="header">//headercode</div>
    <div id="container">
    <div id="mainContent">// your content</div>
    <div id="sidebar">// some side bar stuff</div>
    </div>
    <div id="footer">//footer code</div>
    </body>
    </html>


    External somejs.js file

    display the url base

    alert(siteUrl);

    Because siteUrl loads before any external js, you can just use the siteUrl variable to access the base url. so if you wanted to alert the path to the image using the same structure as shown in my previous post your need to use the code below in your external js file

    alert(siteUrl+'/images/myimages.jpg');

    Does that help

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases