Welcome Guest, Not a member yet? Register   Sign In
locations of images within an external javascript file
#1

[eluser]IAFYM[/eluser]
Hi new to CI,

Trying to convert an existing website to CI and MCV

I've got locations to images within an external js file -- I know the paths are relative to the index.php however, when redirected to another view, these images are no longer loaded... they only work on the first page view.

not only this, but I've got ajax (in the same file using xmlhttprequest, but also, lets say when loading another view (index.php/main/index) it breaks the images on the ajax, even though the paths are relative to the index.php.

I'm really not sure how I go about fixing this.

(sorry if this doesn't make sense)
#2

[eluser]IAFYM[/eluser]
Ok, so I've just re-read that, and it doesn't make much sense.

so, I've got an external javascript file (functions.js) which is called in the head of the main view with
Code:
src="<?=base_url();?>js/functions.js"

within that javascript file, I have the location to an image ("down.gif") which I use in a jQuery menu with fancy effects on it.

The image is located with in the same folder as the js file (js/). The issue I've got is that its only loading when I load up the first view localhost/ci/index.php if I click on any of the working links I've got so far (localhost/ci/index.php/ukcourier/sameday) the image no longer loads.

I'm really not too sure how I can fix this, does anyone have any ideas?
#3

[eluser]Peter Pumpkineater[/eluser]
use absolute paths
#4

[eluser]IAFYM[/eluser]
See, the issue with absolute paths is that there will be a lot of absolute paths to change when the website goes live.

Guess that will just have to work for the moment.
#5

[eluser]Peter Pumpkineater[/eluser]
you can also put your JS file into a views/view.php, add dedicated controller that will return its content with proper header and in html header use that controller uri as JS file source.
#6

[eluser]RellimTtocs80[/eluser]
Another thing that you may be able to do is look at the source code and click on the image source link (when viewing in FF3) or paste the image url in the address bar directly. It may come up saying that the file cannot be found (404 error) since CI is trying to route the image request to the index.php page causing the img src to be incorrect.

If this is what is happening add the following to the .htaccess file to allow CI to know not to rewrite to the index.php file

Code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Basically it checks to see if the uri is a valid directory/file and will not rewrite if the directory/file is valid
#7

[eluser]InsiteFX[/eluser]
Replace $ with s for script.
Try this, this will set a global base_url for javascript. Place in your head section of html.
Code:
<$cript type="text/javascript">
    //&lt;![CDATA[
        base_url = '&lt;?php echo base_url();?&gt;';
    //]]>
</$cript>

Now in your javascript just reference base_url

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB